dte66654 2018-08-03 10:15
浏览 24
已采纳

PHP OOP:创建全局消息数组

I am trying to display an array of messages at the end of my PHP class. My message handler is working, but only if I "add_message" from within the main parent class and not if I call this function from within a child class. Sorry if this is vague but was not sure how to word the question.

TLDR; How can I add a message from within class Example?


MAIN PARENT CLASS

class Init {

    public function __construct() {
        $this->load_dependencies();
        $this->add_messages();
        $this->add_msg_from_instance();
    }

    private function load_dependencies() {
        require_once ROOT . 'classes/class-messages.php';
        require_once ROOT . 'classes/class-example.php';
    }

    public function add_messages() {
        $this->messages = new Message_Handler();
        $this->messages->add_message( 'hello world' );
    }

    // I Would like to add a message from within this instance....
    public function add_msg_from_instance() {
        $example = new Example();
        $example->fire_instance();
    }

    public function run() {
        $this->messages->display_messages();
    }

}

MESSAGE HANDLER

class Message_Handler {

    public function __construct() {
        $this->messages = array();
    }

    public function add_message( $msg ) {
        $this->messages = $this->add( $this->messages, $msg );
    }

    private function add( $messages, $msg ) {
        $messages[] = $msg;
        return $messages;
    }


    // Final Function - Should display array of all messages
    public function display_messages() {
        var_dump( $this->messages );
    }

}

EXAMPLE CLASS

class Example {
    public function fire_instance() {
        $this->messages = new Message_Handler();
        $this->messages->add_message( 'Hello Universe!' ); // This message is NOT being displayed...
    }
}
  • 写回答

2条回答 默认 最新

  • dou448172583 2018-08-03 13:18
    关注

    Because you want to keep the messages around different object, you should pass the object or use a static variable.

    I would use a static variable like so:

    class Init {
        public function __construct() {
            $this->load_dependencies();
            $this->add_messages();
            $this->add_msg_from_instance();
        }
    
        private function load_dependencies() {
            require_once ROOT . 'classes/class-messages.php';
            require_once ROOT . 'classes/class-example.php';
        }
    
        public function add_messages() {
            // renamed the message handler variable for clarity
            $this->message_handler = new Message_Handler();
            $this->message_handler->add_message( 'hello world' );
        }
    
        // I Would like to add a message from within this instance....
        public function add_msg_from_instance() {
            $example = new Example();
            $example->fire_instance();
        }
    
        public function run() {
            $this->message_handler->display_messages();
        }
    }
    
    class Message_Handler {
        // use a static var to remember the messages over all objects
        public static $_messages = array();
    
        // add message to static
        public function add_message( $msg ) {
            self::$_messages[] = $msg;
        }
    
        // Final Function - Should display array of all messages
        public function display_messages() {
            var_dump( self::$_messages );
        }
    }
    
    class Example {
        public function fire_instance() {
            // new object, same static array
            $message_handler = new Message_Handler();
            $message_handler->add_message( 'Hello Universe!' );
        }
    }
    
    // testing...
    new Init();
    new Init();
    $init = new Init();
    $init->add_msg_from_instance();
    $init->add_msg_from_instance();
    $init->add_msg_from_instance();
    $init->run();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 深度学习运行代码直接中断
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢