大佬教程收集整理的这篇文章主要介绍了PHP动态类加载,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的值对象类如下:
/* file UserVO.PHP*/ class UserVO { public $id; public $email; public function __construct($data) { $this->id = (int)$data['id']; $this->email = $data['email']; } }
我创建了我的数组对象,如下所示:
/* file Userservice.PHP*/ $array = array( array(...),array(...)); $count = count($array); for ($i = 0; $i < $count; $i++) { $result[] = new UserVO($arraY[$i]); } return $result;
好的,所以这一切都很好.但是,我想详细说明要动态创建的VO,以便我可以有一个动态函数来创建我的VO.
就像是:
$ret = create_vo($array,'UserVO'); function create_vo($data,$vo) { $count = count($data); for ($i = 0; $i < $count; $i++) { $result[] = new $vo($data[$i]); //this obv@R_403_2469@sly wont work...Class name must be a valid object or a String } return $result; }
我意识到我可以用switch语句(迭代所有我的VO)…但是毫无疑问是一个更优雅的解决方案.如果我可以根据需要懒惰加载VO,那么它也将是过冷的,而不是拥有多个“包含”
任何帮助非常感谢.
$result[] = new $vo($data[$i]); //this obv@R_403_2469@sly wont work...Class name must be a valid object or a String
你试了吗?它的工作原理就像预期的那样(在PHP 5.1中,我不知道如何在PHP 4中使用OOP,但是如果你使用__construct作为构造函数,那么这个工作).
避免多个包含在使用任何类之前定义魔术函数__autoload
function __autoload($className) { require_once 'myclasses/'.$className.'.PHP'; }
以上是大佬教程为你收集整理的PHP动态类加载全部内容,希望文章能够帮你解决PHP动态类加载所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。