About the difference between a php static method call and an instantiated class call

Source: Internet
Author: User

1. First to clarify some points of view

Since the static method has only one copy in memory, no matter how many times you call it, it is common, and there is no concept of object, so you cannot use the $this call in a static method, and if you have to invoke it, you can only instantiate your own class

And instantiation is not the same, each instantiation is an object, in memory is multiple

<?PHPerror_reporting(E_all|e_strict);classa{ Public functionBar () {Echo' Bar '.Php_eol; }     Public Static functionfoo () {Echo' foo '.Php_eol; }}a::bar ();//Will error a:: foo ();//correct
$obj = new A ();
$obj foo ();//correct


Strict Standards:non-static Method Human::easyeat () should not being called statically in ....
However, a class that is instantiated can call a static method.
*/

<?PHPclassa{ Public $name= ' Zongshuai ';  Public functionBar () {Echo' Bar '.Php_eol; }     Public Static functionfoo () {EchoSelf::$name.Php_eol;//Error}} A:: foo ();/*A static method cannot invoke a non-static property. You cannot use Self:: Call a non-static property. */



Class Access--general method (object method)--not (although the method is not $this keyword, you can!) But this is not supported.)

Object Access General methods (methods of objects), you can

About the difference between a php static method call and an instantiated class call

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.