[英]Calling a method on a object with a variable
誰能解釋以下原因為何不起作用:-
我得到的輸出:-
var_dump($this->_payableItem->getNet());
但是當嘗試通過以下方式訪問它時,我收到一個錯誤:-
$thresholdUnitMethod = 'getNet()';
$this->_payableItem->$thresholdUnitMethod
錯誤:-
PHP注意:未定義的屬性:PayableItem :: $ getNet()在
試試看:
$thresholdUnitMethod = 'getNet';
$this->_payableItem->$thresholdUnitMethod();
這將執行一個調用的函數getNet
上_payableItem
。
您可以這樣使用call_user_func
:
call_user_func(array($this->_payableItem, 'getNet'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.