2012-09-18 22 views
0

我想開發用於OpenCart的模塊,但我是PHP中OOP的新手。我很難解釋OpenCart代碼。解釋Opencart OOPS代碼

我知道以下語句在PHP中意味着什麼,即通過$ this訪問類的方法和變量,這是對調用對象的引用。

$this->custom_function(); 
$this->defined_variable; 

但是我不明白這樣的說法。 $this->config->get('config_template')或這$this->request->get['field']

你們可以幫我理解這一點。如何讀取/解釋?

回答

2
$ans = $this->config->get('config_template') 
// is the same as 
$foo = $this->config; // accessing 'config' property 
$ans = $foo->get('config_template'); // calling 'get' function on object in config var 

$ans = $this->request->get['field']; 
// is the same as 
$bar = $this->request; // accessing 'request' property 
$ans = $bar->get['field']; // accessing 'get' property (which is an array) 

它被稱爲方法/屬性鏈,當您不想爲僅使用一次的對象設置變量時使用它。這與訪問多維數組是一樣的。例如。與你寫的數組$arr['one']['two']['three'],如果陣列是對象,你會寫$obj->one->two->three

請注意,開放購物車的來源是相當醜陋的。我會建議用一些不太複雜的學習和晦澀

+0

所以設置$ foo = $ this-> config; $ foo變成了一個對象或者一個保存$ this-> config的值的變量; – dkjain

+0

'$ foo'變成'$ this-> config'中的任何東西。它可能是一個對象,一個數組,一個資源或其他。在這種特殊情況下,它是一個對象。我們可以確定,因爲有一個方法(函數)調用。 – karka91

1
$this->config->get('config_template') 

可以理解爲:從當前對象($本),使用性能(配置),這是一個對象,並且在配置調用對象的方法來獲得,並將值'config_template'傳遞給函數。

$this->request->get['field'] 

可以讀作:從當前對象($此),可使用性(請求),該一個對象,並從該對象使用陣列(獲得)具有索引「字段」。