fork(1) download
  1. <?php
  2. class Init {
  3.  
  4. public function __construct() {
  5. $this->add_messages();
  6. $this->add_msg_from_instance();
  7. }
  8.  
  9.  
  10.  
  11. public function add_messages() {
  12. $this->messages = new Message_Handler();
  13. $this->messages->add_message( 'hello world' );
  14. }
  15.  
  16. // I Would like to add a message from within this instance....
  17. public function add_msg_from_instance() {
  18. $example = new Example();
  19. $example->fire_instance();
  20. }
  21.  
  22. public function run() {
  23. $this->messages->display_messages();
  24. }
  25.  
  26. }
  27. class Message_Handler {
  28.  
  29. public function __construct() {
  30. $this->messages = array();
  31. }
  32.  
  33. public function add_message( $msg ) {
  34. $this->messages = $this->add( $this->messages, $msg );
  35. }
  36.  
  37. private function add( $messages, $msg ) {
  38. $messages[] = $msg;
  39. return $messages;
  40. }
  41.  
  42.  
  43. // Final Function - Should display array of all messages
  44. public function display_messages() {
  45. var_dump( $this->messages );
  46. }
  47.  
  48. }
  49. class Example {
  50. public function __construct() {
  51. $this->messages = new Message_Handler();
  52. $this->messages->add_message( 'Hello Universe!' ); // This message is NOT being displayed...
  53. $this->messages->display_messages();
  54. }
  55. }
  56. $test = new Example();
Success #stdin #stdout 0.02s 23560KB
stdin
Standard input is empty
stdout
array(1) {
  [0]=>
  string(15) "Hello Universe!"
}