-3

Is the magic method __set called when setting properties in __construct?

class MyClass
{
    public function __construct()
    {
        $this->property = 'something';
    }
    public function __set($name,$value)
    {
        $this->{$name} = ($name == 'property')?'other value':$value;
    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
olanod
  • 30,306
  • 7
  • 46
  • 75
  • 1
    I'd guess so, would not be hard to run that code and see for yourself? – Esailija Oct 25 '12 at 09:33
  • 1
    Why don't you just instantiate the class (no other method calls) and put an echo into the __set, that should tell you. – Martin Lyne Oct 25 '12 at 09:34
  • It was a sudden doubt i had, I cant test it now but it seemed so simple that I thought php sages over here would know without a doubt. – olanod Oct 25 '12 at 09:40
  • I take it you are not aware of sites like codepad? If you are able to post this question, you are also able to test the code :P – Esailija Oct 25 '12 at 09:42

1 Answers1

1

This is perfectly legal to do. From the manual:

__set() is run when writing data to inaccessible properties.

It does not matter if it is done from the constructor.

so invoking:

$x = new MyClass();
var_dump($x);

will result in:

object(MyClass)#1 (1) { ["property"]=> string(11) "other value" }
JvdBerg
  • 21,777
  • 8
  • 38
  • 55