匿名类构建 [英] Anonymous class construction
问题描述
我需要一个想法来创建PHP上的匿名类。
$ b >
class {}
); <在PHP上,你没有类范围(除了在命名空间,但它有同样的问题下面);
class $ name {}
); PECL。
我需要什么,为什么:
好吧,我需要创建一个名为 create_class()
的函数,它接收一个键名和一个匿名类 。它对我有用,因为我想使用PHP不能接受的不同的名称类符号。例如:
<?php
create_class('it.is.an.example' ,function(){
return class {...}
});
$ obj = create_object('it.is.an.example');
?>
所以,我需要一个接受这个用法的想法。我需要它,因为在我的框架我有这个路径: /modules/site/_login/models/path/to/model.php
。因此, model.php
需要声明一个名为 site.login / path.to.model
的新类。 / p>
调用 在PHP 7.0中, 匿名课程我不完全了解您的问题,但您的 这将实例化一个具有匿名类类型的对象,并将其注册到 I need an idea to create anonymous class on PHP. I don't know how I can works. See my limitations: What I need, and why: Well, I need create a function called ie So, I need an idea that accept this use. I need it because on my framework I have this path: On call In PHP 7.0 there will be anonymous classes. I don't fully understand your question, but your This will instantiate an object with an anonymous class type and register it into the 这篇关于匿名类构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! create_object()
如果内部缓存有 $ class
定义(如 it.is.an.example
它只是返回新的类对象,如果没有,需要加载,所以我将使用 create_class()
函数可能如下所示:
function create_class(string $ key,array& $ repository){
$ obj = new class($ key){
private $ key;
function __construct($ key){
$ this-> key = $ key ;
}
};
$ repository [$ key] = $ obj;
return $ obj;
}
$ repository
。要获取对象,请使用您创建的对象: $ repository ['it.is.an.example']
。
class {}
);class $name {}
);runkit
PECL.create_class()
that receives a key name and a anonymous class. It'll be useful for me because I want use different name class symbols that PHP can't accept. For instance:<?php
create_class('it.is.an.example', function() {
return class { ... }
});
$obj = create_object('it.is.an.example');
?>
/modules/site/_login/models/path/to/model.php
. So, the model.php
need to declare a new class called site.login/path.to.model
.create_object()
if the internal cache have a $class
definition (like it.is.an.example
it simply return the new class object. If not, need load. So I will use the $class
content to search fastly what is the class file.create_class()
function might look like this:function create_class(string $key, array &$repository) {
$obj = new class($key) {
private $key;
function __construct($key) {
$this->key = $key;
}
};
$repository[$key] = $obj;
return $obj;
}
$repository
. To get an object out you use the key you created it with: $repository['it.is.an.example']
.