簡體   English   中英

如何基於php中的數組值動態創建對象?

[英]How can I create dynamically objects based on array values in php?

我想動態地創建$ instance數組中存在的對象(例如domain1_comdomain2_com ),並為它們指定數組值的名稱(例如domain1_comdomain2_com ),以便我可以通過這些名稱訪問它(例如domain1_com->example() )。

可能嗎? 我嘗試了類似的方法,但顯然不起作用。

    class myClass
    {
        public static function getInstances()
        {
            // I connect to the database and execute the query
            $sql = "SELECT * FROM my_table";    
            $core = Core::getInstance();
            $stmt = $core->dbh->prepare($sql);

            if ($stmt->execute()) {
                $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            }

            // Read values in my array
            foreach ($results as $instance) {

                $obj = $instance["domain"]);
                // return 2 values: domain1_com and domain2_com

                $obj = new myClass();
            }
        }

        public function example()
        {
            echo "This is an instance";
        }
    }

    myClass::getInstances();

    $domain1_com->example();
    $domain2_com->example();

您可以使用變量變量。

$name = "foo";
$$name = new bar();

是相同的

$foo = new bar();

您不能在該方法之外訪問在getInstances內部創建的變量。 它們是本地的,而不是全球的。

試試這個代碼:

class myClass
{
    public static function getInstances()
    {
        $results = array('domain1_com', 'domain2_com');

        foreach ($results as $instance) {
            $$instance = new myClass();
            // This is equivalent to "$domainX_com = new myClass();".
            // Writing that code here would define a _local_ variable named 'domainX_com'.
            // This being a method inside a class any variables you define in here are _local_,
            // so you can't access them' _outside_ of this method ('getInstances')
        }

        // this will work, we are inside 'getInstances'
        $domain1_com->example();
        $domain2_com->example();
    }

    public function example()
    {
        echo "This is an instance";
    }
}

myClass::getInstances();

// this won't work. $domainX_com are not accessible here. they only exist _inside_ 'getInstances'
// It's basic OOP.
// so this code will crash
$domain1_com->example();
$domain2_com->example();

它將產生以下輸出:

這是一個實例

這是一個實例

E_NOTICE:類型8-未定義的變量:domain1_com-在第32行

E_ERROR:類型1-在第32行上的非對象上調用成員函數example()

您需要一種訪問這些變量的方法。 我會用這個:

class myClass
{
    private static $instances = array();
    public static function getInstances()
    {
        $results = array('domain1_com', 'domain2_com');

        foreach ($results as $instanceName) {
            self::$instances[$instanceName] = new myClass();
        }
    }

    public static function getInstance($instanceName) {
        return self::$instances[$instanceName];
    }

    public function example()
    {
        echo "This is an instance";
    }
}

myClass::getInstances();

// this will work
myClass::getInstance('domain1_com')->example();
myClass::getInstance('domain2_com')->example();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM