douzhanhui5662 2016-10-08 11:46
浏览 17
已采纳

自动加载类

I've built "core" class that loads another classes, and I want to load automatically all the classes in spesific folder named "class", I've started to build something, but I have no idea if it's good.
In the construct function at the core class, I'm getting an array with the class names to load.
The construct function calls to function named _loadClasses, and in the _loadClasses function,
I'm loading the classes by using require_once() fucntion.
Then at the top of the page, i'm adding public variable with the name of the class.
For example, "public $example;"
Now, what left is to create the ocject of the class, so that's what I did.
Example of the _loadClasses method:

require_once("class/user.class.php");
self::$user = new User();

Here comes the "automat" part.
I want the function _loadClasses to get an array, for example:

private function _loadClasses( $classesToLoad = array('security', 'is') );

and now, I'm using glob to load the classes from the folder "class", the name syntax of the classes files in the folder "class" is classname.class.php.

$classesArray = array(); // initialize the variable of all the web classes

    $classesFiles = glob("class/*.php"); // gets all the web classes from the folder 'class'

    foreach($classesFiles as $file) { // loop on the classes in the folder 'class'
        $filename = explode('class/', $file);
        $filename = $filename[1];
        $className = explode('.class.php', $filename);
        $className  = $className[0];

        if($className != 'index.php' || $className != 'database') {
            array_push( $classesArray, $className ); // adds the class name into the array 'classesArray'
        }
    }

    foreach( $classesArray as $className ) {
        if( in_array($className, $classesToLoad) ) {
            require_once("class/$className.class.php");
            $classLines = file( "class/$className.class.php" );
            $classNameLine = $classLines[1];
            $classNameLine = explode(' ', $classNameLine);
            $classObjectName = $classNameLine[1];
            $classObjectName = explode(" ", $classObjectName);


            self::$$classObjectName = new $classObjectName();
        }
    }

I need something like that, of curse it doesn't work, it's just to show you what I wanna do with an example.
Thanks in advance.

展开全部

  • 写回答

3条回答 默认 最新

  • dqsw7529 2016-10-08 13:15
    关注

    For this particular approach I'd suggest something like:

    // Your custom class dir
    define('CLASS_DIR', 'class/')
    
    // Add your class dir to include path
    set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);
    
    // You can use this trick to make autoloader look for commonly used "My.class.php" type filenames
    spl_autoload_extensions('.class.php');
    
    // Use default autoload implementation
    spl_autoload_register();
    

    To get started there's no need to implement a parent class autoloading functionality for "core" objects since they should only be aware of their role functionality. Use php standard library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 潜在扩散模型的Unet特征提取
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部