I am trying to define a namespace/autoload structure for multiple projects. Each project has a library. The consists classes which will be extended for each project. Here is the content of one project
- /project1
- /lib
- parentclass.php
- autoloader.php
- index.php
- myclass.php
- /lib
parentclass.php:
<?php
class parentclass{
}
?>
myclass.php
<?php
class pr1_myclass extends parentclass{
}
In index.php i will define a global array with settings which will be used in the autoloader:
<?php
$settings = array(
'dir' => __DIR__;
'lib_dir' => 'project1_dir',
'lib_namespace' => '??',
);
$project_prefix = 'pr1';
$GLOBALS['projectsAutoloadSettings'][$project_prefix] = $settings;
The autoloader will be something like this:
<?php
function my_autoloader($className){
$parts = explode('_',$className);
if(isset($GLOBALS['projectsAutoloadSettings'][$parts[0])){
$settings = $GLOBALS['projectsAutoloadSettings'][$parts[0]];
//I now have settings array for this project. From here i can include the right file:
include($settings['dir'].'/'.$parts[1].'.php');
}else{
//Need to include library file. This is part of my problem. I dont know how to handle this
}
}
spl_autoload_register('my_autoLoader');
This was the structure for 1 project. I have multiple projects. The /lib directory in each project is the same.
Now i need a namespace structure which will work in both 2 environments mentioned below, while only having to change the $settings.
- Local developer environment. Here a single /lib should be used for all projects. So for example a class from project 3 will extends a parent class from the library from project 1.
- Active environment. Here each class in a project extends the library from its project. So a class from project 3 will always extend a parent class from project 3.
Multiple projects can be active at a single web page. I figure i need namespaces for this, but my main question is that the /lib directory is always the same code (copy pasted). I want to keep it that way, as far as i know it is not possible to have a different namespace for /project3/lib than for project1/lib.
How can i still achieve my goal. I am interested in the code-structure, things like autoloader code (classname to filename) i will figure out myself.
EDIT: An edit with an example of a problem i am facing, which seems not clear if i look at the comments.
It is in live (not developer) mode. I have a second project with the same structure as project one, but instead with this class in /project2/someclass.php
<?php
class pr2_someclass extends parentclass{
}
?>
Now this is my code for running both projects:
<?php
//project 1 start:
$myClass1 = new pr1_myclass();
//This loads pr1_myclass which is an extension from the parent class in project1/lib/parentclass.php.
//So far so good
//Project 2 start
$myClass2 = new pr2_someclass();
//Here the problem occurs. This class loads pr2_someclass, but it will be an extension of project1/lib/parentclass.php instead of project2/lib/parentclass.php.
?>
Since both lib directories are identical, you might think i just should use the same library for both projects. However, there can be more than a year between two projects so it might be possible that the two libraries are not exactly identical.