dounuo7954 2016-06-01 12:51
浏览 33
已采纳

动态实例化包含在数组中的PHP类

I'm trying to instantiate some classes in an array. This is the scenario: I have many classes, e.g.:

class Class0 extends NumberedClasses{...}
class Class1 extends NumberedClasses{...}
class Class2 extends NumberedClasses{...}

They will increase over the time, so instead of instantiating them this way:

$instances0 = new Class0();
$instances1 = new Class1();
$instances2 = new Class2();

I want to have a getter method, like this one:

function getStrategies(){
   return array(Class0, Class1, Class2);
}

So I can just add classses in that array in the future, and call it this way:

$strategies = $this->getStrategies();
for ($strategies as $strategy) { 
    $instance = new $strategy();
}

I'm used to do something similar with js, where I have the window object, so I can instantiate classes even just with a string (new window["Class1"]). But I'm new with PHP and can't find the way to do that.

Thanks in advance,

  • 写回答

1条回答 默认 最新

  • dpcj32769 2016-06-01 13:57
    关注

    It may be suggested that using an Intermediary Class to manage these instances will be much ideal. Consider this:

        <?php
    
            class NumberedClasses{}
            class Class0 extends NumberedClasses{}
            class Class1 extends NumberedClasses{}
            class Class2 extends NumberedClasses{}
    
    
            class InstanceHelper {
    
                /**
                 * @var array
                 */
                protected $instances = array();
    
                public function addInstance($instanceKey, $instance) {
                    if(!array_key_exists($instanceKey, $this->instances)){
                        $this->instances[$instanceKey] = $instance ;
                    }
                }
    
                public function addInstances(array $arrayOfInstances) {
                    foreach($arrayOfInstances as $instanceKey=>$instance){
                        if(!array_key_exists($instanceKey, $this->instances)){
                            $this->instances[$instanceKey] = $instance ;
                        }
                    }
                }
    
                public function removeInstance($instanceKey) {
                    if(array_key_exists($instanceKey, $this->instances)){
                        unset($this->instances[$instanceKey]);
                    }
                    return $this->instances;
                }
    
                public function getAllInstances() {
                    return $this->instances;
                }
            }
    

    Now; with the Class InstanceHelper, You can easily manage your instances... even add Instances, remove Instance and so on.... Again consider this tests:

        <?php
    
            $insHelper          = new InstanceHelper();
            $instances          = array(
                'c0'    =>  new Class0(),
                'c1'    =>  new Class1(),
                'c2'    =>  new Class2(),
            );
    
            $insHelper->addInstances($instances);
    
            var_dump($insHelper->getAllInstances());
            var_dump($insHelper->removeInstance('c1'));
    

    Here are the results of both var_dumps:

            ::VAR_DUMP 1::
            array (size=3)
              'c0' => 
                object(Class0)[2]
              'c1' => 
                object(Class1)[3]
              'c2' => 
                object(Class2)[4]
    
    
    
            ::VAR_DUMP 2::
            array (size=2)
              'c0' => 
                object(Class0)[2]
              'c2' => 
                object(Class2)[4]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?