且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP动态类加载

更新时间:2023-12-05 07:54:34

$result[] = new $vo($data[$i]); //this obviously wont work...Class name must be a valid object or a string

试过了?它的工作原理正如预期的(在php 5.1,我不知道它是如何与OOP在PHP 4,但如果你使用 __构造的构造函数,这应该工作)。

Have you tried it? It works just as expected (in php 5.1, I don't know how was it with OOP in php 4, but if you are using __construct for constructor this should work).

为避免多次使用,请在使用任何类之前定义魔术函数 __ autoload

To avoid multiple includes define magic function __autoload before using any class

function __autoload($className)
{
    require_once 'myclasses/'.$className.'.php';
}

所以先调用 new UserVo 将触发此函数包含文件 myclasses / UserVo.php

So first call new UserVo will trigger this function to include file myclasses/UserVo.php.