对使用 class 文件名快捷方式 OpenCart 感到困惑

Confused about using class file name shortcut OpenCart

我是 PHP 的新手,我在 opencart PHP 引擎中遇到过奇怪的事情。
有一个名为 catalog/controller/module/slideshow.php.
的文件 此 class 扩展 Controller class

class ControllerModuleSlideshow extends Controller {
        protected function index($setting) {
                static $module = 0;

                $this->load->model('design/banner');
                $this->load->model('tool/image');
.....
                 $this->model_design_banner->getBanner($setting['banner_id']);
.....

$this->model_design_banner这个class里没有这个成员,哦可能是在父class.
里 让我们检查一下,cd....

<?php
abstract class Controller {
        protected $registry;
        protected $id;
        protected $layout;
        protected $template;
        protected $children = array();
        protected $data = array();
        protected $output;

嗯.... ??!!! WTF(抱歉)

这个class也没有这样的成员......

我猜这是 catalog/controller/module/banner.php

的快捷方式

// 这里有很多问题 how , where ?

让我们打开catalog/model/design/banner.php

<?php
class ModelDesignBanner extends Model {
        public function getBanner($banner_id) {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner_image bi LEFT JOIN " . DB_PREFIX . "banner_image_description bid ON (bi.banner_image_id  = bid.banner_image_id) WHERE bi.banner_id = '" . (int)$banner_id . "' AND bid.language_id = '" . (int)$this->config->get('config_language_id') . "'");

                return $query->rows;
        }
}

好的,没有以前的问题,它看起来很正常...停止!

如果我们可以调用方法快捷方式,它应该是静态的...。

$this->db->query

在这种情况下应该什么也没有....

对我来说有很多奇怪的事情

这是如何运作的。快捷方式如何映射到函数,为什么函数不是静态的等等。

请解释一下,如有任何帮助,我将不胜感激。

编辑

如果加载对象在我的 class 中加载模型的方法声明了加载方法,那么它也应该在同一个 class.

使用 $this->model_design_banner 失败,因为 $this->load->model('design/banner'); 失败。

它失败了,因为你混淆了 "model" 和 "module"。

您在模块文件夹中有一个模型文件:

catalog/module/design/banner.php

这应该是

catalog/model/design/banner.php

函数调用 $this->load->model('design/banner'); 尝试从特定位置加载模型:"catalog/model/design/banner.php"。但是在您的情况下找不到它,因此 "magic shortcut" 不起作用。只需将文件移动到正确的文件夹即可。 $this->model_design_banner->getBanner(); 找到模型后应该可以工作。

这在内部是如何工作的?

model() function expects the model file with a model class in a certain folder. It will then load this file (if it exists) and instantiate the model class as a class property of your current class. To build the name of the class property, the name of the model class is modified from ModelAAABBB to model_aaa_bbb and that's $this->model_aaa_bbb - for easier usage. This is not really documented in depth (http://docs.opencart.com/developer/loading),但它在内部是这样工作的。

看看 Loader.php with model(): https://github.com/opencart/opencart/blob/master/upload/system/engine/loader.php#L15

这是静态文件加载,结合注册表模式和 Magic 属性 从控制器访问。有一种 "easier"、"non-magic" 方式:简单地依赖自动加载。这将允许直接在控制器中使用 $model = new ModelDesignBanner();。自动加载器会通过它的类映射将类名解析为文件名。这真的取决于……这是 OpenCart 核心团队的设计决定。我更喜欢自动加载方法,因为它不会隐藏太多真正发生的事情。如果地图真的很大,它可能比直接包含更慢。

魔法不好 - http://www.infoq.com/presentations/8-lines-code-refactoring