duankun9280 2016-01-18 09:26
浏览 51

具有静态库的项目的自动加载和命名空间结构

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

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀