簡體   English   中英

從另一個類訪問類的功能

[英]Access to a function of a class from another class

正如標題所說,我的情況如下:

require_once("connect.php") //database connection file

class one {

    private $mysqli;

    function __construct ($dbi) {
        $this -> mysqli = $dbi;
    }

    function one {
        // ... function using things like $this -> mysqli -> prepare and so on...
    }
}

並且,在同一個文件中:

class two {
    private $mysqli;

    function __construct ($dbi) {
        $this -> mysqli = $dbi;
    }

    function two {
        // here I need to access the function "one" of the class "one"
        // If i do something like $one = new one ($mysqli) I get an error on the __construct
    }
}

我真的很生氣,但我相信這並不困難,因為我是PHP的初學者。 希望那里的人可以幫助我。

忽略mysqli問題不應該是這樣的

$one = new one ($this->mysqli);

$two = new two($this->mysqli);
$two->two($one->one); // call pass function from one into two

並且你將2的聲明改為類似的

function two($functiontorun) {

現在我不是在PHP中的OO專業版(沒有看到非亂序非編譯語言中的點)但我相信你也可以通過將class 2作為class 1的EXTENDS來解決這個問題。或者如果你把你的1級公開並且一個公共的功能然后只要第1級用new one等實例化,然后你應該在你的函數內部兩個能夠只調用$one->one(); 但我不是百分之百

雖然我不太確定我是否理解你的問題(因為所有MySQL的東西),但是可以使用public關鍵字來訪問方法(=對象中的函數):

    public function functionName( $parameter ) {
        \\ function stuff
    }

暫無
暫無

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

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