views:

29

answers:

2

Hi all,

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);    
A: 

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.

Mikulas Dite
hi Mikulas, it's posted now :)
Rolf
A: 

OK, the problem was from somewhere else, I had something like

public function setDisplayAll(boolean $value), and it seems that this boolean typing for the method argument was triggering an error (and this class was used to manage errors ...)

sorry, thanks for your help anyway and have a good day !

Rolf