How does a PHP static method call a non-static method of the parent class?
淡淡烟草味
淡淡烟草味 2017-06-08 11:01:40
0
5
1071

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?

淡淡烟草味
淡淡烟草味

reply all(5)
Ty80

self::get_one_by_sql

为情所困

First make sure the get_one_bysql of the parent class is static

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

迷茫

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);
巴扎黑

Cannot be called.

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

滿天的星座

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template