Because it has already been said what the difference is, I will try to explain the goals.
These magic functions are actually made for some code design. Some people find that calling $object->property clearer than calling $object->getProperty() .
It is more clear when you use the chain, i.e. object->getDatabase()->getTable()->getRow()->getType()->convertToString()->makeUppercase() , etc. The use of "hidden properties" is more understandable.
Using magic methods does not solve some problems (for example, calling by reference), but sometimes they are easier to read.
Using properties is very popular in some languages (e.g. Object Pascal, i.e. Delphi or FreePascal), where your class may have something like:
property count: Integer read getCount write getCount;
where both getCount() and setCount() become private or protected. An encoder may think of them as variables, but methods are called instead.
In PHP, there are also some advantages of the magic methods __get() (and __set() ). Usually, when you call a nonexistent property, a notification will be given. But if you implement the __get() method, no notification is given, and you can encode the __get() method, for example, to return NULL anyway if this property does not exist or handles errors or something else.
For what danger (but also opportunities) it may be, please take a look at the last example here .
source share