簡體   English   中英

使用get_object_vars()更新類的所有公共變量的值

[英]Update values of all public variables of class using get_object_vars()

我有100位公共成員上課。 如何自動更新它們,即不指定其名稱。 我已經嘗試過了,並且正在獲取變量,但是所做的更改未反映在實際對象上。 請指教。

    class foo {
    public $b = 1;
    public $c = 2;


    function __construct()
    {
        $x = get_object_vars($this);
        foreach ($x as $obj) {
                 $obj = 9;
        }
    }
}

$test = new foo;

echo $test->c;

它將vale打印為2而不是9

function __construct()
{
    $x = get_object_vars($this);
    foreach ($x as $key => $value) {
        $this->$key = 9;
    }
}

暫無
暫無

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

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