简体   繁体   English

从父类扩展的子PHP类如何将值添加到父类数组属性上?

[英]How can a child PHP class which extends from a parent class, add values on to the parent classes array property?

In this simple mock-up PHP class below I have 2 methods for adding JavaScript files into a page. 在下面的这个简单的模拟PHP类中,我有2种将JavaScript文件添加到页面中的方法。

The goal is to use this in my apps plugin system so that user plugins which will extend this PluginCore class below will be able to call a function allowing them to include JavaScript files from there plugin into the main app. 目标是在我的应用程序插件系统中使用此功能,以便将在下面扩展此PluginCore类的用户插件能够调用一个函数,使他们可以将插件中的JavaScript文件包含到主应用程序中。

PluginCore has an an Array $this->jsFilesArray which is used to store all the JavaScript files that should be included into the page. PluginCore有一个数组$this->jsFilesArray ,该数组用于存储应包含在页面中的所有JavaScript文件。

There is a setter method to add new JS files to this array. 有一个setter方法可将新的JS文件添加到此数组。

There is also a method which will iterate over the array and insert each JavaScript file into the page with script tags. 还有一种方法可以遍历数组,并使用脚本标签将每个JavaScript文件插入页面。


My question now is, how can I allow any plugin classes which extend from PluginCore class be able to somehow get there JS files added into the parent classes $this->jsFilesArray array so that when the core class inserts the files into the page, it will include any plugin JS files that have called a function of some sort to add files? 现在我的问题是,如何允许从PluginCore类扩展的所有插件类都能够以某种方式将JS文件添加到父类$this->jsFilesArray数组中,以便当核心类将文件插入页面时,会包括任何已调用某种功能来添加文件的插件JS文件吗?

One idea I had is to use a WordPress style plugin/hook functionality so that the plugins would basically call a hook that is fired from the PluginCore class and inserts the JS files into the page. 我的一个想法是使用WordPress样式的插件/挂钩功能,以便插件从根本上调用从PluginCore类触发的挂钩,并将JS文件插入页面。 THE child plugins would simply hook into that action event. 子插件将仅挂接到该动作事件。

This method will work but I was hoping to find other methods to achieve my desired result for learning purposes and also to test different methods. 这种方法可以使用,但是我希望找到其他方法以达到学习目的所需的结果,并测试不同的方法。

Another idea is to have the child plugins have a addJsFile() function which would return an array of JS files for that plugin when called. 另一个想法是让子插件具有addJsFile()函数,该addJsFile()在调用时将返回该插件的JS文件数组。 The parent class could then call this method on the child class and do so for each plugin while combining the results of all them into a single array and passing that one into the PluginCore class. 然后,父类可以在子类上调用此方法,并针对每个插件执行此方法,同时将所有插件的结果合并为一个数组,然后将其传递给PluginCore类。

So how can I achieve this? 那么我该如何实现呢?


Mock-up Class code to give a visual of what it might look like? 模拟类代码可以直观地看到它的外观?

class PluginCore {

    public $jsFilesArray = array();


    public function __construct () {
    $this->addJsFile('jsFileCOre1', 'http://file-path-url-to-js-file/jsfilecore1.js');
    }



    public function addJsFile($jsFileKey, $jsFileUrl) {
    if( ! isset($this->jsFilesArray[$jsFileKey] )){
        $this->jsFilesArray[$jsFileKey] = $jsFileUrl;
    }
    }



    public function loadJsFilesIntoDom() {
    foreach($jsFiles as $fileKey => $fileValue){
        echo '<script type="text/javascript" src="'.$fileValue['fileUrl'].'"></script>\n';
    }
    }

}

One way is to make it static : 一种方法是使其static

class PluginCore {

    public static $jsFilesArray = array();


    public function __construct () {
        self::addJsFile('jsFileCOre1', 'http://file-path-url-to-js-file/jsfilecore1.js');
    }



    public static function addJsFile($jsFileKey, $jsFileUrl) {
        if( ! isset(self::$jsFilesArray[$jsFileKey] )){
            self::$jsFilesArray[$jsFileKey] = $jsFileUrl;
        }
    }



    public function loadJsFilesIntoDom() {
        foreach(self::$jsFilesArray as $fileKey => $fileValue){
        echo '<script type="text/javascript" src="'.$fileValue['fileUrl'].'"></script>\n';
    }
    }

}

class PluginShell extends PluginCore {

    public function __construct() {
        self::addJsFile('jsFileShell1', 'http://file-path-url-to-js-file/jsfileshell1.js');
    }



}

$core = new PluginCore();
$shell = new PluginShell();

var_dump(PluginCore::$jsFilesArray);
var_dump(PluginShell::$jsFilesArray);

This way $jsFilesArray is not bound to the instance of the class but the class itself so you don't have to worry about the communication between both classes. 这样, $jsFilesArray不会绑定到类的实例,而是绑定到类本身,因此您不必担心两个类之间的通信。

But you might want to read about when to use static methods (and when not). 但是您可能想了解何时使用静态方法(何时不使用静态方法)。 Here the answer of Ionuț G. Stan is very interesting and may help you further with your class if static is not the way you want to go. 在这里 ,Ionuț G. Stan的答案非常有趣,如果您不希望采用static方法,那么它可能会帮助您进一步上课。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM