简体   繁体   中英

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.

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 has an an Array $this->jsFilesArray which is used to store all the JavaScript files that should be included into the page.

There is a setter method to add new JS files to this array.

There is also a method which will iterate over the array and insert each JavaScript file into the page with script tags.


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?

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. 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. 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.

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 :

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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