fork download
  1. <?php
  2. class Show_message {
  3. public $message_instance = ""; //ensure Message object variable is visible
  4. function __construct() {
  5. $this->message_instance = new Message();
  6. }
  7. function display_message() {
  8. //instatiate Message object
  9. echo $this->message_instance->message . " : in display_message function <br>"; //works
  10. }
  11. function display_again() {
  12. echo $this->message_instance->message . " : in display_again function <br>"; //does not work
  13. }
  14. }
  15. class Message {
  16. public $message = ""; //ensure $this->message variable is visible?
  17. function __construct() {
  18. $this->message = "Hello world"; //make message
  19. }
  20. }
  21. $instance = new Show_message(); //instatiate Show_message object
  22. $instance->display_message(); //method to create instance and display message
  23. $instance->display_again(); //method to display message again
  24. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Hello world : in display_message function <br>Hello world : in display_again function <br>