2016-09-12 3 views
0

하나의 메인 클래스와 '플러그인'이라는 별도의 클래스가 있습니다. 이러한 플러그인에는 이벤트가 트리거 될 때 호출되는 메소드가 포함될 이벤트 시스템이 있습니다. 주 클래스의 다른 인스턴스를 만들거나 __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(); 

작동합니다

여기에서 자세한 내용을 찾을 수 있습니다