且构网

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

从变量创建类名

更新时间:2022-04-17 08:43:43

查看 class_alias - 它应该可以帮助您更清楚这一点!

Take a look at class_alias - it should help you make this even readable!

:你使用正常名称创建类,然后根据需要添加别名。别名可以是任何字符串,包括存储在变量中的字符串。

Just to make this clear: You create your class with a "normal" name, then add an alias name on demand. The alias name can be any string, this includes a string stored in a variable.

编辑

虽然我认为, class_alias()是要走的路,这里是一个例子,如何做没有它。

While I think, that class_alias() is the way to go, here is an example on how to do it without it.

从您的OQ:

class MyModel_Rc_posts extends MyModelController {
         // code here
}

现在

eval("class MyModel_{$PREFIX}posts extends MyModel_Rc_posts {};");

应该执行类似于 class_alias()

如果你需要 get_class()给你原来的(静态)类名,而不是别名。使用丑陋 eval() / extends 骗子你会得到动态名称。

Going this rout could be necessary, if you need get_class() later - on an aliased class, it will give you the original (static) classname, not the alias. With the ugly eval()/extends trick you get the dynamic name.