Why should PHP magic methods be publicly available?

I use magic methods in my PHP classes, but when I try to make them private, I warn:

WARN: the magic __get () method must have public visibility and cannot be static in ...

I would not want to have these methods in Eclipse autocomplete. (maybe a path with phpdoc?) So my question is: why should these methods be publicly available?

+5
source share
1 answer

Because you are calling methods from an area outside the class.

For instance:

// this can be any class with __get() and __set methods
$YourClass = new YourOverloadableClass();

// this is an overloaded property
$YourClass->overloaded = 'test';

The above code is "converted" to:

$YourClass->__set('overloaded', 'test');

Later, when you get the property value, for example:

$var = $YourClass->overloaded;

This code translates to:

$YourClass->__get('overloaded');

__get __set , public.

+7

All Articles