douqiaolong0528 2018-11-12 19:44
浏览 79

PHP Overload类由其他具有相同名称和命名空间的类

I have a directory tree where in different sub-directories I have a lot of classes with the same name. There is a strong intention to not edit these classes.

I'm looking for a way to load one class and after using it and destroying its instance load another with exactly the same name. Then use it, destroy its instance and repeat that process.

I thought some possible solutions:

  1. Loading class with the same name that replaces previously loaded class (Overloads it)
  2. Unloading a class before I load class with the same name but from different path
  3. Creating a new class (dynamically created class body) under different name or by adding to it namespace. Creation process firstly reads source class body, its methods, properties and "copies" that to new class. Similar to clone on instances but done on class body level.
  4. Read the first class, instantiate it, use its methods, destroy its instance. Then remove all methods inside first class and dynamically create inside it all methods that are read from the second class having the same name.
  5. Read file content of a class and create temporary file with the class content you read but change class name or put unique namespace on top of it and finally load temporary file. (Least appealing approach to me)

For the 3rd I thought this could be useful: Componere or Runkit or Classkit but don't have any experience with them.

Do you have any other ideas or perhaps some solutions? Did you use componere or Runkit / Classkit and can say they suit the job? maybe there are other options?

Perhaps there is a OOP design pattern that covers this issue but I'm not familiar with it.

Example code:

<?php

#------------------------------------
//path Foo/Bar.php

/* class is without a namespace or have the same
as Baz/Bar.php */
class Bar
{
    public function getName() : string
    {
        return 'Foo/Bar';
    }
}

#------------------------------------
//path Baz/Bar.php

/* class is without a namespace or have the same
as Foo/Bar.php */
class Bar
{
    public function getName() : string
    {
        return 'Baz/Bar';
    }
}

#------------------------------------
//path ./Execute.php

$paths = [
    'Foo/Bar.php',
    'Baz/Bar.php'
];

$results = [];
foreach ($paths as $path) {

    //how to create instance of class Bar in Foo/Bar.php and then in Baz/Bar.php
    //without PHP Fatal error:  Cannot declare class...
    $classDynamic = ...

    $results[] = $classDynamic->getName();
    unset($classDynamic);
}

var_export($results)

/**
 * prints
 * array('Foo/Bar', 'Baz/Bar')
 */
  • 写回答

1条回答 默认 最新

  • dsf12313 2018-11-12 20:03
    关注

    I do not think there is a way to unset a class once it has been defined and as they share the same namespace they will error out. but you could get the effect you wanted as follows

    class Bar
    {
        $myname = "";
        function __construct($setname="")
        {
           $this->myname = $setname;
        }
        function getName()
        {
           return $myname;
        }
    }
    
    $paths = array('Foo/Bar','Baz/Bar');
    $results = array();
    foreach($paths as $p)
    {
       $bit = new Bar($p);
       $results[] = $bit->getName();
    }
    var_export($results);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器