工作原理:PHP中的魔术方法,并通过逻辑而非定义来理解它们


How it works: Magic Methods in PHP and understanding them by logic not by definitions

大家好!我有以下例子(来自Opencart):

home.php:

<?php
class ControllerCommonHome extends Controller {
    public function index() {
        $this->document->setTitle($this->config->get('config_meta_title'));
        $this->document->setDescription($this->config->get('config_meta_description'));
        $this->document->setKeywords($this->config->get('config_meta_keyword'));
        if (isset($this->request->get['route'])) {
            $this->document->addLink(HTTP_SERVER, 'canonical');
        }
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/home.tpl', $data));
        } else {
            $this->response->setOutput($this->load->view('default/template/common/home.tpl', $data));
        }
    }
}

controller.php:

<?php
abstract class Controller {
    protected $registry;
    public function __construct($registry) {
        $this->registry = $registry;
    }
    public function __get($key) {
        return $this->registry->get($key);
    }
    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
}

我无法通过逻辑流程来理解这是如何工作的:

  1. "document"和setTitle(…)在哪里?$this->document->setTitle($this-->config->get('config_meta_title'));

  2. "config"和get(…)在哪里?$this->document->setDescription($this-->config->get('config_meta_description'));

这是怎么回事?我想不出这个过程。。。我可以读到它,但在尝试重做时有空格。

加载或设置属性时调用__set__get
$this->property = 'value'; // calls __set
echo $this->property; // calls __get. 

您的get()请求由__call 处理

$this->get('foo'); // calls __call
Object::get('foo'); // calls __callStatic

请参阅有关魔术方法的文档:http://php.net/manual/en/language.oop5.overloading.php#object.call

此外,在您的控制器示例中,任何__get或__set都被重定向到注册表类方法getset

因此,如果在您的示例$中,这是一个扩展抽象类Controller:的类

$this->title = 'foo'; // redirected to registry set
echo $this->title; // redirected to registry get

从你发布的代码中很难识别文档是什么。然而,文档是通过注册表get加载的。因此:

// request
$this->document->setTitle('foo');
// calls magic method __get
// calls $this->registry->get('document');
// returns the value in registry for document, which might be an object; let's hope so
// now run the method setTitle('foo') on the object returned from the registry

PHP __get和__set magic方法本质上是在引用不存在的对象的属性时调用的。然后,您可以使用属性/属性名称作为键(作为参数传递给这些函数)来返回您想要的任何内容。在上面的情况下,它根据键(属性/属性名称)从注册表返回一个对象。

要找到源"文档"对象,您首先需要了解它是如何在注册表中注册的。这很可能发生在应用程序引导过程中。我不熟悉OpenCart,但会有一些项目在注册表中注册。一旦你发现,你将能够看到什么样的对象被实例化并存储到注册表中。