Static and non-static things in PHP

Source: Internet
Author: User
Static and non-static things in PHP

Static can also invoke non-static methods

Talk less, look directly at the code:

Error_reporting (E_all);//Send out all error messages class Test{     Public Static  function test1() {        //$this->test2 ();//This must be an error, static method can not use $this        //If you do not want to call Test2 () here, you can use the following method, called Mode one        $instance=NewTest ();$instance->test2 ();//or use a simpler way, called Way two (PHP will give a call warning)         Self:: Test2 (); } Public  function test2() {        Echo ' test static function '; }}test::test1 ();

Here are a few things to explain,
1. Mode One this means creating an object and then calling the method of the object, which does not conflict.
2. As to how mode two can call the non-static method, please refer to an article by Brother Bird: Php calling Scope
Mode two, which produces the following warning:

All of us in the actual application, everyone try to avoid using "::" to invoke a non-static method.

Non-static method call static method so beauty

Think I am also drunk, unexpectedly can also use $this to invoke non-static method. And look at the code:

Error_reporting (E_all); class Test2{     Public Static  function test1() {        Echo ' Good test
'; }//Can you imagine why I don't write test2 () This method name? Straight from 1 Tigger to 3. Public function test3() { Self:: Test1 ();//This is OK, no problem Static:: Test1 ();//This doesn't explain either. $this->test1 ();//focus on it .}}$OBJ=NewTest2 ();$OBJ->test3 ();

I was surprised to write code to verify that this $this was able to invoke a static method. It is reasonable to think carefully, why is it reasonable?
And listen to the younger brother slowly.

Have you ever called code like this:

$objnew Test2();$obj->test1();

OK, you can call a non-static method in PHP statically, or you can call a static method in a non-static way. What a fair language it is (che) dead (dan) you!
If you can understand the above writing, I believe that it $this->test1() should be understood, the $this equivalent of the current invocation of the object, it can be understood as the new out here $obj . As an object must be able to invoke all the methods inside it? Don't explain, say more is nonsense.

  • Related Article

    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.