Maison  >  Article  >  développement back-end  >  php __call and __callStatic

php __call and __callStatic

巴扎黑
巴扎黑original
2016-11-24 13:38:091087parcourir

PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法。如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用。

__call() is triggered when invoking inaccessible methods in an object context.

__callStatic() is triggered when invoking inaccessible methods in a static context.

<?php
class MethodTest {
    public function __call($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "Calling object method &#39;$name&#39; "
             . implode(&#39;, &#39;, $arguments). "\n";
    }
    /**  As of PHP 5.3.0  */
    public static function __callStatic($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "Calling static method &#39;$name&#39; "
             . implode(&#39;, &#39;, $arguments). "\n";
    }
}
$obj = new MethodTest;
$obj->runTest(&#39;in object context&#39;);
MethodTest::runTest(&#39;in static context&#39;);  // As of PHP 5.3.0
?>


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:php获取本地实际IPArticle suivant:php 获取 POST JSON 数据