PHP class not storing reference

后端 未结 3 1421
[愿得一人]
[愿得一人] 2021-01-12 15:21

How do I pass a reference to an object constructor, and allow that object to update that reference?

class A{
    private $data;
    function __construct(&         


        
3条回答
  •  感情败类
    2021-01-12 15:44

    You'll have to tell PHP to assign a reference also to the private member data like this:

    $this->data = &$d;
    

    Depending on the context, you may not want to use references to external arrays, and it might be better to have that array inside an object that handles it.

    Aslo notice that the constructor is called __construct not __construction.

提交回复
热议问题