How to check the magic method or not?

I am trying to find a check of a magic method in a reflection class, but it is not. Perhaps php (I am using php 5.3) has some other tools to solve this problem? Something like that:

class myClass {

    public function __call($method, $arguments)
    {
        return 'is magic';
    }

    public function notMagic()
    {
        return 'not a magic';
    }

}


$reflection = new ReflectionMethod('myClass', 'magic');

if ($reflection->isMagic())
{
    /* do something if is magic*/
}
+4
source share
1 answer

Since PHP does not provide a way to check if a method is magic or not, you have two options.

Documents say that

PHP reserves all function names starting with __ as magic. it is recommended that you do not use function names from __ to PHP if you need documentary magic functionality.

Therefore, you can simply check if the method name begins with __:

if(strpos($methodName, '__') === 0){
    echo "$methodName is magic";
}

, - __myNewMethod, , , PHP .

. , , :

__construct(), __destruct(), __call(), __callStatic(), 
__get(), __set(), __isset(), __unset(), __sleep(), 
__wakeup(), __toString(), __invoke(), __set_state() and __clone() 

, PHP , .

, , , .

PHP

+2

Source: https://habr.com/ru/post/1526501/


All Articles