Is it possible to handle 'magic' property assignments a'la PHP?

Gary Willoughby dev at nomad.so
Sat Jan 4 11:08:44 PST 2014


In D is it possible to handle accessing class properties that 
don't exist, similar to PHP's magic methods?

http://www.php.net/manual/en/language.oop5.overloading.php#object.set

For example, the below class doesn't have the 'x' property:

class T
{
     this()
     {
         this.x = "hello world!";
     }
}

Can i handle this in a programmatic way to intercept the property 
and deal with it?

Something like this:

class T
{
     this()
     {
         this.x = "hello world!";
     }

     opMagic(A, B)(A property, B value)
     {
         // property = "x", value = "hello world!"
     }
}


More information about the Digitalmars-d-learn mailing list