Static and non-static tasks in PHP

Source: Internet
Author: User
In PHP, static and non-static things can also call non-static methods. let's just look at the code: error_reporting (E_ALL); Report all error messages to classTest {publicstaticfunctiontest1 () {$ this-& gt; test2 (); in this case, an error is returned. static and non-static tasks in $ this PHP cannot be used for static methods.

Static methods can also be called.

Simply look at the code:

Error_reporting (E_ALL); // report all error messages to class Test {public static function test1 () {// $ this-> test2 (); // this is definitely an error. the static method cannot use $ this // If you have to call test2 () here, you can use the following method, method 1 $ instance = new Test (); $ instance-> test2 (); // or use a simpler method called Method 2 (PHP will give a call warning) self:: test2 ();} public function test2 () {echo 'test static function';} test: test1 ();

Here are some notes,
1. Method 1: Create an object and call the method of this object. This does not conflict with each other.
2. for details about how method 2 can call non-static methods, refer to the article: PHP Calling Scope
Method 2: generate the following warning:

In practical applicationsTry to avoidUse ":" To call a non-static method.

The call of static methods by non-static methods is so coquetry.

I think I am also drunk and can still use $ this to call non-static methods. Check the code:

Error_reporting (E_ALL); class Test2 {public static function test1 () {echo 'Good test
';} // You can imagine why I did not write the test2 () method name here? Jump from 1 to 3 public function test3 () {self: test1 (); // you can do this, no problem static: test1 (); // this does not explain $ this-> test1 (); // focus on it} $ Obj = new Test2 (); $ Obj-> test3 ();

I was taken aback by writing code verification when $ this was actually able to call static methods. It is reasonable to think about it. why?
Listen to your younger brother.

Have you ever called the code like this:

$obj = new Test2();$obj->test1();

OK. in php, you can call non-static methods in static mode, or call static methods in non-static mode. It's a fair language. I Love You (che) and you are dead (dan!
If you can understand the preceding method$this->test1()It should be understandable,$thisIt is equivalent to the currently called object and can be understood as the new$obj. As an object, you can certainly call all its internal methods, right? I don't want to explain it. it's all nonsense.

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.