我想为OpenCart开发模块,但我不熟悉PHP中的OOP。我在解释OpenCart代码时遇到困难。
我知道以下语句在PHP中意味着什么,即通过$ this访问类的方法和变量,这是对调用对象的引用。
$this->custom_function();
$this->defined_variable;
然而我不理解这样的陈述。 $this->config->get('config_template')
或此$this->request->get['field']
等。
答案 0 :(得分: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
请注意,打开购物车来源非常难看。我建议学习一些不太复杂和模糊的东西
答案 1 :(得分:1)
$this->config->get('config_template')
可以读作:从当前对象($ this),使用属性(config)作为对象并在方法获取的config对象中调用,并将值'config_template'传递给函数。
$this->request->get['field']
可以读作:从当前对象($ this),使用属性(请求)作为对象,并从该对象使用带有索引'field'的数组(get)。