dongshi3605 2010-06-23 14:40
浏览 32

PHP分段错误| 在类中为受保护的属性调用__get?

I have a class with the methods __toString and __get and some protected properties such as "message". So far, so good.

The problem now is that I need to access $this->message in __toString, and this causes (NOT ALWAYS BUT OFTEN) a segmentation fault when (see following example) $display_all is set to true. Do you know why and how to fix it ?

Thanks a lot ! Rolf

PS: here is an example

class FuckedClass {
    protected $file;
    protected $line;
    protected $display_all;
    protected $message;

    //[...]

    /**
     * Magic getter
     * @param String $name
     * @return mixed
     */
    public function __get($name) {
        return  (in_array($name,array_keys(get_class_vars(__CLASS__))))?
                    $this->$name : null;
    }
    /**
     * Formats
     */
    public function __toString() {
        $message = $this->message . ($this->display_all) ?
                 '[ Location: '.$this->file.' @ line '.$this->line.' ]':
                 '';
        $text =<<<PLAIN
Error : {$message}
PLAIN;
        return $text;
    }
}

//instantiated $fucked_class
die($fucked_class);    
  • 写回答

2条回答 默认 最新

  • dsb12300 2010-06-23 14:48
    关注

    Actually, __toString is just a method called when variable is being typed to string, or the method itself is called. It behaves as any other class method, so this code must work:

    class Foo {
        protected $message = "bar";
    
        public function __toString()
        {
            return $this->message;
        }
    }
    

    Or you may have some problem in the __get, please post it's contend and I'll edit my answer.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路