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条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办