"Very sick." The PHP Class instantiation object can access the class's static Method!!!

Source: Internet
Author: User
Tags php class

Previously found a PHP pervert problem: static (statically) call in PHP non-static method of the detailed

This time to see the next thinkphp source function.inc.php , there is a function:

/** * Get object instance support call static method of class * * @param string $name class name * @param string $method method * @param string $args parameter * @return Object Instance */function get_instance_of ($name, $method = ", $args = Array ()) {Static $_instance = array (); $identify = Empty ($args) ? $name. $method: $name. $method. To_guid_string ($args), if (!isset ($_instance[$identify)) {if (class_exists ($name)) {$o = new $name (); if (method_exists ($o, $method)) {if (!empty ($args)) {$_instance[$identify] = Call_user_func_array (Array (& $o, $method), $args);} else {$_instance[$ Identify] = $o $method ();}} else {$_instance[$identify] = $o;}} else {halt (L (' _class_not_exist_ '). ‘:‘ . $name);}} return $_instance[$identify];}

The function annotation says that it can support the static method of calling the class , from the source surface, the class instance is a static method that cannot invoke the class. However, PHP has to support class instantiation objects to access static methods, but cannot access static properties.

<?phpini_set (' Display_error ', true); error_reporting (e_all); class Dog {public static $name = ' Wangwang '; static function Say ($data) {echo $data;}} $myDog = new Dog (); $myDog->say (' 123456 '); Output 123456echo $myDog->name; Send notice message: Undefined Property:dog:: $name in ...? >

Reference: http://php.net/manual/zh/language.oop5.static.php

"Very sick." The PHP Class instantiation object can access the class's static Method!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.