簡體   English   中英

PHP構造函數通過引用傳遞對象

[英]PHP constructor return object with pass by reference

我正在研究一個PHP類,該類將獲取另一個類並以某種方式返回它,顯然構造函數無法返回值,因此我正在查看按引用傳遞,這就是我得到的:

<?php
class Example {
    public function __construct($name,&$var){
        //This registers the class in my system (I know this part works)
        IceTray::$Registry->registerLibrary($name);
        //this fetches the object of the registered class above (I know it works)
        $var = IceTray::$Registry->Libraries->$name;
    }

我通過引用傳遞的錯誤嗎? 因為當我在項目中使用它時:

$test = 0;
$lib = new Example('ClassName', $test);
$test->testing();

$ Test是我希望存儲對象的變量,構造函數的第一個參數是要注冊並分配給通過引用傳遞的變量(第二個參數)的類的名稱。 下一行在所請求的類名內稱為方法,但它不起作用。

沒有錯誤或其他任何內容,同樣,我還是引用參考概念的新手,也許我做錯了什么。 非常感謝您的任何幫助,在此先感謝您!

class Example {
    public $var = null;
    public function __construct($name){
        //This registers the class in my system (I know this part works)
        IceTray::$Registry->registerLibrary($name);
        //this fetches the object of the registered class above (I know it works)
        $this->var = IceTray::$Registry->Libraries->$name;
    }
}

$lib = new Example('ClassName');
$lib->var->testing();

為什么要傷害自己而不使用調用屬性返回需要的內容?

暫無
暫無

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

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