Hello. Im' wondering if this is possible:
I've successfully used __set() magic method to set values to properties of a class:
class View
{
private $data;
public function __set( $key, $value )
{
$this->data[$key] = $value;
}
}
So I'm able to:
$view = new View();
$view->whatever = 1234;
The problem comes when I want to concatenate a string for example. It seems like __set() is not being called (it's not being called in fact).
$view = new View();
$view->a_string = 'hello everybody'; //Value is set correctly
$view->a_string.= '<br>Bye bye!'; //Nothing happens...
echo $view->a_string;
This outputs "hello everybody". I'm not able to execute __set() in the second assignment. Reading php.net it says that:
__set() is run when writing data to inaccessible properties.
So as *a_string* already exists, __set is not called. My question finally is... how could I achieve that concatenation operation??
Note: Ok... Murphy came and gave me the answer as soon as I posted this...
The answer (As I understood), is that PHP is not able to decide if *a_string* is available as I didn't defined a __get() method.
Defining __get() allows php to find the current value of *a_string*, then uses __set() to concatenate the value.