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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据