search

Home  >  Q&A  >  body text

How does a PHP static method call a non-static method of the parent class?

1. This calls the parent class method:

2. Self calls the parent class method:

3. Parent calls the parent class method:

Isn’t there a way for subclass static methods to call parent class non-static methods?

淡淡烟草味淡淡烟草味2771 days ago1112

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-06-08 11:03:40

    self::get_one_by_sql

    reply
    0
  • 为情所困

    为情所困2017-06-08 11:03:40

    First make sure the get_one_bysql of the parent class is static

    About using https://stackoverflow.com/que...

    reply
    0
  • 迷茫

    迷茫2017-06-08 11:03:40

    What you are using is obviously very irregular. Of course, let’s get to the point first.
    To adjust the non-static method of the parent class, first you have to get the class instance
    If there is a cache, just get it directly, if not, create one

    $instance = new self();
    $totalCount = $instance->get_one_bysql($sqlstr);

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-08 11:03:40

    Cannot be called.

    Non-static methods need to have the $this object, and this object cannot be provided when calling from static methods.

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-08 11:03:40

    Open the comments and play around to find out

    <?php
    class a{
        public $ab = NULL;
        public function d(){
            var_dump($this->ab);
        }
        public function c($a,$b){
            var_dump($a+$b);
        }
    }
    
    class b extends a{
        public static function t(){
            // $this->d();          //Fatal error: Using $this when not in object context in D:\phpStudy\WWW\index.php on line 14
            // $this->c(1,2);          //Fatal error: Using $this when not in object context in D:\phpStudy\WWW\index.php on line 15
            // self::d();             //Strict Standards: Non-static method a::d() should not be called statically in D:\phpStudy\WWW\index.php on line 16
                                    //Fatal error: Using $this when not in object context in D:\phpStudy\WWW\index.php on line 5
            // self::c(1,2);        //Strict Standards: Non-static method a::c() should not be called statically in D:\phpStudy\WWW\index.php on line 18 
                                    // int(3)
            // parent::d();            //Strict Standards: Non-static method a::d() should not be called statically in D:\phpStudy\WWW\index.php on line 20
                                    // Fatal error: Using $this when not in object context in D:\phpStudy\WWW\index.php on line 5
            // parent::c(1,2);        //Strict Standards: Non-static method a::c() should not be called statically in D:\phpStudy\WWW\index.php on line 22
                                    //int(3)
        }
    }
    b::t();

    Summary: this is used for instance calls. self, parent are used to call static properties or methods.
    The last strange phenomenon is that although statically calling the non-static method of the parent class to process data will get an error, int(3) is still output

    reply
    0
  • Cancelreply