我遇到了可用的会话消息系统here的问题,其中没有显示消息。经过大量的反复试验后,我决定简化我为了测试而遇到以下问题:
<?php
ini_set('error_reporting', E_ALL);
define('script_access', true);
if (!isset($_SESSION)) {
session_start();
}
require('../framework/classes/messages.php');
$msg = new Messages();
class Test {
public $foo;
public function __construct() {
$this->foo = new Messages();
}
public function create_form() {
if (isset($_POST['submit']) == 'Submit') {
$this->form_process();
}
echo '<form action="' . $_SERVER['PHP_SELF'] . '?id=1&table=about" method="post">';
echo '<input name="submit" type="submit" id="submit" value="Submit">';
echo '</form>';
}
public function form_process() {
//$new = new Messages();
$this->foo->add('s', 'new message from');
header("Location: message.php?proc=true");
}
}
if ($_GET['proc'] == true) {
echo 'should be a message here<br>';
echo $msg->display();
exit;
} else {
$test = new Test();
$test->create_form();
}
?>
经过一番乱搞后,我将现在注释掉的$new = new Messages();
添加到子程序中,并显示消息。然而,留下它注释掉它们没有出现。我不知道为什么我必须重新声明我已经在构造函数中声明的类。有没有人知道为什么会发生这种情况以及我如何才能使它只需要在构造函数中而不是在子例程中启动类?
答案 0 :(得分:0)
嗯,好吧,问题是在测试类中发起的类消息只是“活着”在那里:
<?php
ini_set('error_reporting', E_ALL);
define('script_access', true);
if (!isset($_SESSION)) {
session_start();
}
require('../framework/classes/messages.php');
$msg = new Messages();
class Test {
public $foo;
public function __construct() {
$this->foo = new Messages();
}
public function getmessage() {
return $this->foo->display();
}
public function create_form() {
if (isset($_POST['submit']) == 'Submit') {
$this->form_process();
}
echo '<form action="' . $_SERVER['PHP_SELF'] . '?id=1&table=about" method="post">';
echo '<input name="submit" type="submit" id="submit" value="Submit">';
echo '</form>';
}
public function form_process() {
$this->foo->add('s', 'new message from');
header("Location: message.php?proc=true");
}
}
$test = new Test();
if ($_GET['proc'] == true) {
echo 'should be a message here<br>';
echo $test->getmessage();
exit;
} else {
$test->create_form();
}
?>