douchunsui2395 2012-05-26 09:46
浏览 50
已采纳

从另一个类中引用类时,未定义的属性和未定义的索引

I am currently working on a large project for a client, and to help simplify things and make managing sessions easier I have made a session class, this means that if I ever change the way I manage my sessions I can access the structure of the sessions in one file rather then multiple files where a session has been echoed (Such as the users name or the current page)

My class has 3 types of functions setters, getters and unsetters which is kind of self explanatory.

Now in this instance I am setting an error message to the session class directly and then getting the error messages from another class which calls a function from the session within it.

Below are simplified versions on my files showing code I feel is relevent

c_session.php

class session {

    private $instance = 'isntname';
    private $A_user = 'user';
    private $A_page = 'page';
    private $A_message = 'message';

    //Set messages to the session.
    public function set_message($type, $value) {
        $_SESSION[$this->instance][$this->A_message][$type] = $value;
    }

    //Get messages from the session.
    public function get_message() {     
        return $_SESSION[$this->instance][$this->A_message];
    }

    //Unset messages from the session.
    public function unset_message() {
        #unset($_SESSION[$this->instance][$this->A_message]);
    }

}

c_operations.php

class operations {

    //Display all pending messages.
    public function display_pending_messages() {

        if(session::get_message() != null) {
            foreach(session::get_message() as $type => $value) {
                if(strlen($type) != null) {
                    echo '
                        <div class="panel ' . $type . '">
                            ' . $value . '
                            <span class="close" onclick="hide_parent_element(this);">X</span>
                        </div>
                        ';
                }
            }
            session::unset_message();
        }
    }

}

example.php

$session->set_message('confirm', 'THIS IS A CONFIRM');
$session->set_message('error', 'THIS IS AN ERROR');
$session->set_message('notice', 'THIS IS A NOTICE');
$session->set_message('warning', 'THIS IS A WARNING');

var_dump($_SESSION);

$operation->display_pending_messages();

Errors/notices etc received

Notice: Undefined property: operations::$instance in /var/www/_classes/parent_session.php on line 43 
Notice: Undefined property: operations::$A_message in /var/www/_classes/parent_session.php on line 43 
Notice: Undefined index: in /var/www/_classes/parent_session.php on line 43

Line 43 refers to the line return $_SESSION[$this->instance][$this->A_message];

Now if I call get_message() and unset_message() directly via $session->get_message() it works as expected but going through another function in this case $operation->display_pending_messages() it returns the above errors. Obviously this has something to do with the $this-> operator but I'm not to sure on what to do to stop this. I have tried various searches and while finding something similar it wasn't helpful in this case.

Can someone please explain where I've gone wrong and how to fix this?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dsa88886666 2012-05-26 09:55
    关注

    In class operations you call your session as static but all your data is in an object. The best way is to store in a static, all data of your class session : ` class session {

    private static $instance = 'isntname';
    private static $A_user = 'user';
    private static $A_page = 'page';
    private static $A_message = 'message';
    
    //Set messages to the session.
    public static function set_message($type, $value) {
        $_SESSION[self::instance][self::A_message][$type] = $value;
    }
    
    //Get messages from the session.
    public static function get_message() {     
        return $_SESSION[self::instance][self::A_message];
    }
    

    }

    And so you can call all your functions with session:: in your code, without create object session

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。