dsft8327 2017-01-09 01:15
浏览 80

基于会话的“flash”消息未显示

I'm experiencing an issue with a session message system available here where the messages aren't being displayed. After a lot of trial and error I decided to simplify the issue I was experiencing down to the following for the purposes of testing:

<?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();
}
?>

After some messing around I added the now commented out $new = new Messages(); to a subroutine and the messages appear. However, leaving it commented out they do not appear. I'm not sure why I have to redeclare a class that I've already declared in the constructor. Does anybody know why this is happening and how I can make it so that I only have to initiate the class in the constructor instead of in the subroutine?

  • 写回答

1条回答 默认 最新

  • drug95107 2017-01-09 02:57
    关注

    Hmm alright, the issue was it that the class messages initiated inside the test class was only 'living' there:

    <?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();
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错