匿名类构建 [英] Anonymous class construction

查看:132
本文介绍了匿名类构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个想法来创建PHP上的匿名类。


$ b >
  • 在PHP上,您不能创建匿名类,例如匿名函数(如 class {} );

  • <在PHP上,你没有类范围(除了在命名空间,但它有同样的问题下面);
  • 在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>

    调用 create_object()如果内部缓存有 $ class 定义(如 it.is.an.example 它只是返回新的类对象,如果没有,需要加载,所以我将使用

    解决方案

    在PHP 7.0中, 匿名课程我不完全了解您的问题,但您的 create_class()函数可能如下所示:

      function create_class(s​​tring $ 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']


    I need an idea to create anonymous class on PHP. I don't know how I can works.

    See my limitations:

    • On PHP you can't make anonymous class, like anonymous function (like class {});
    • On PHP you don't have class scope (except in namespaces, but it have the same problem below);
    • On PHP you can't use variables to specify the class name (like class $name {});
    • I don't have access to install the runkit PECL.

    What I need, and why:

    Well, I need create a function called ie 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');
    
    ?>
    

    So, I need an idea that accept this use. I need it because on my framework I have this path: /modules/site/_login/models/path/to/model.php. So, the model.php need to declare a new class called site.login/path.to.model.

    On call 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.

    解决方案

    In PHP 7.0 there will be anonymous classes. I don't fully understand your question, but your 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;
    }
    

    This will instantiate an object with an anonymous class type and register it into the $repository. To get an object out you use the key you created it with: $repository['it.is.an.example'].

    这篇关于匿名类构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆