dongshan6870 2019-03-15 18:29
浏览 130
已采纳

php从对象类添加方法到另一个对象类

i have a object like this:

CORE::$ObjClassInstABS['ABS']['DATA']

that contains an array of Class->Method:

array (
  'DATA' => 
  array (
    'USERDATAMANAGER' => 
    Class_UserdataManager::__set_state(array(
    )),
    'PRODDATAMANAGER' => 
    Class_ProddataManager::__set_state(array(
    )),
  ),
)

i create a new object, of type class Like this:

CORE::$ObjClassInstABS['ABS']['ABSDATAMANAGER'] = new class;

i cant but need pass all the methods of the first object, ignoring the class of origin to the class i create on fly, and that allows me to execute the functions from the class declared on the fly. does this exist in php 7.0 or is there any way to achieve this reach?? It would be like cloning the methods of several classes to a single and new class.

Answer for @Damian Dziaduch comments

the piece of code that i used to Dynamically Instance all class file from a directory is this, and populate the first object with instance of class:

CORE::$ObjClassInstABS['ABS']['ABSDATAMANAGER']= new class;    
foreach (CORE::$ObjClassABS['DATA'] as $key => $name) {
    if (strpos($name, 'class.') !== false) {
        $name  = basename($name);
        $name  = preg_replace('#\.php#', '', $name);
        $names = explode(".", $name);
        foreach ($names as $key => $namesr) {
            $names[$key] = ucfirst(strtolower($namesr));
        }
        $name                                         = implode('_', $names);
        $NamesClass                                   = $name . 'Manager';
        $InstanceClass                                = strtoupper(preg_replace('#\Class_#', '', $NamesClass));
        CORE::$ObjClassInstABS['ABS']['DATA'][$InstanceClass] = $this->$InstanceClass = new $NamesClass();
    }
}

the result of it is the Array printed at start of the post CORE::$ObjClassInstABS['ABS']['DATA'] .

if you see at start of foreach i have the new class declaration to use, in loop, how can i populate CORE::$ObjClassInstABS['ABS']['ABSDATAMANAGER'] in the loop, it with all methods of the first object instance, and make it executables?

that i whant (not work):

foreach ( CORE::$ObjClassInstABS['ABS']['DATA'] as $key => $value ) {
    CORE::$ObjClassInstABS['ABS']['ABSDATAMANAGER'] .= Clone($value);
}

$value represent where is storing the methods:

 ::__set_state(array()),
  • 写回答

1条回答 默认 最新

  • dpfwhb7470 2019-03-15 19:04
    关注

    As requested.

    Not sure whether this will fill you requirements... The question is whether you are able to overwrite the CORE::$ObjClassInstABS

    <?php
    
    CORE::$ObjClassInstABS = new class extends \ArrayIterator {
        private $container = [];
    
        public function __construct(array $container)
        {
            $this->container = [
                'ABS' => [
                    'DATA' => [
                        'USERDATAMANAGER' => new class {},
                        'PRODDATAMANAGER' => new class {},
                    ],
                ],
            ];
        }
    
        public function offsetExists($offset)
        {
            return isset($this->container[$offset]);
        }
    
        public function offsetGet($offset)
        {
            return isset($this->container[$offset]) ? $this->container[$offset] : null;
        }
    
        public function offsetSet($offset, $value)
        {
            if (is_null($offset)) {
                $this->container[] = $value;
            } else {
                $this->container[$offset] = $value;
            }
        }
    
        public function offsetUnset($offset)
        {
            unset($this->container[$offset]);
        }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败