我环顾四周寻求帮助。我意识到这可能更多地与我使用对象的方式有关(我是 OO PHP 的新手),但这真的让我很烦恼。这是我正在尝试做的一个大大简化的版本:
<?php
class Show_message {
public $message_instance = ""; //ensure Message object variable is visible
function __construct() {
//do nothing
}
function display_message() {
$message_instance = new Message(); //instatiate Message object
echo $message_instance->message . " : in display_message function <br>"; //works
}
function display_again() {
echo $message_instance->message . " : in display_again function <br>"; //does not work
}
}
class Message {
public $message = ""; //ensure $this->message variable is visible?
function __construct() {
$this->message = "Hello world"; //make message
}
}
$instance = new Show_message(); //instatiate Show_message object
$instance->display_message(); //method to create instance and display message
$instance->display_again(); //method to display message again
?>
为什么函数$message_instance->message
不可见display_again()
?