douzhanhui5662 2016-10-08 19: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 21: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 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码