php动态类继承 [英] php dynamic class inheritance
问题描述
我知道我可以通过执行在运行时生成一个类
I know I can generate a class at runtime by executing
$obj = (object)array('foo' => 'bar');+
这样我可以使用
echo $obj->foo; //bar
如果要使$ obj继承自现有类怎么办?
What if want to make $obj inherits from an existing class?
我想实现的目标: 我正在GitHub上的巴黎项目( https://github.com/balanza/paris ).这是一个活跃的记录班.我想知道我是否需要为每个对象声明一个类,即使它为空:
What I wanna achive: I'm forking paris project on github (https://github.com/balanza/paris). It's an active record class. I wonder I need to declare a class for every object, even if it's empty:
class User extends Model{}
我想我可能会使用动态对象来避免这些无聊的事情.
I guess I might use dynamic object to avoid this boring stuff.
推荐答案
您总是可以执行eval('class User extends Model{}')
,但这不是一个好主意.确实,您应该只在文件中创建该类,然后操作码缓存才能正常工作,可以对其进行版本跟踪等.
You could always do eval('class User extends Model{}')
but is not a good idea. Really, you should just create the class in a file, then opcode caching will work properly, it can be version tracked, etc etc.
tl; dr:定义模型类,它是正确的事情".
tl;dr: Define the model class, it is the Right Thing To Do™.
这篇关于php动态类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!