2016-09-12 22 views
0

私は1つのメインクラスと 'プラグイン'と呼ばれる別のクラスを用意します。これらのプラグインには、イベントがトリガされたときに呼び出されるメソッドが含まれるイベントシステムがあります。メインクラスの別のインスタンスを作成したり、__​​constructでメインクラスを提供したりすることなく、プラグインクラスからメインクラスの関数にアクセスする方法はありません。PHPクラスオブジェクトを保存/保存する

答えて

0

あなたのPHPバージョンに応じて、特性を使用することができます。継承されたクラスまたは関連性のないクラスにも共通の機能を提供します。私は次の構造を作成しiliazによって投稿答えを使用して

http://php.net/manual/en/language.oop5.traits.php

0

をし、それがこの出力

This is from the main class. 
This is from the plugin in the main class 
This is from the plugin in the main PLUGIN class 
This is from the plugin 
に取得完全

<?php 

class MainClass { 

    use MainTrait; 

    function __construct() { 
     $this->fromMainClass(); 
     $this->initPlugins(); 
    } 
} 

trait MainTrait { 


    private function initPlugins(){ 
     new PluginClass(); 
    } 

    function fromMainClass(){ 
     echo "This is from the main class.<br>"; 
    } 

    function callFromPlugin(){ 
     echo "This is from the plugin in the main class<br>"; 
    } 

} 

class MainPluginClass { 

    use MainTrait; 

    function pluginTest(){ 
     echo "This is from the plugin in the main PLUGIN class<br>"; 
    } 

} 

class PluginClass extends MainPluginClass{ 

    function __construct() { 
     $this->callFromPlugin(); 
     $this->pluginTest(); 
     $this->plugin(); 
    } 

    function plugin(){ 
      echo "This is from the plugin<br>"; 
    } 

} 

new MainClass(); 

作品:

あなたはここで多くを見つけることができます

関連する問題