亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP 靜態(tài)方法怎么調(diào)用父類非靜態(tài)方法?
淡淡煙草味
淡淡煙草味 2017-06-08 11:01:40
0
5
1237

1、this調(diào)用父類方法:

2、self調(diào)用父類方法:

3、parent調(diào)用父類方法:

難道子類靜態(tài)方法沒有辦法調(diào)用父類非靜態(tài)方法嗎?

淡淡煙草味
淡淡煙草味

全部回復(fù)(5)
Ty80

self::get_one_by_sql

為情所困

先確定下父類的get_one_bysql 是不是靜態(tài)的

關(guān)于使用 https://stackoverflow.com/que...

迷茫

你這么用的顯然很不規(guī)范, 當(dāng)然先說正題.
要調(diào)父類的非靜態(tài)方法, 首先你得取到類實例
如果有緩存就直接拿, 沒有就創(chuàng)建一個

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

調(diào)用不了.

非靜態(tài)方法需要有$this對象, 從靜態(tài)方法調(diào)用提供不了這個對象.

滿天的星座

打開注釋玩玩就知道了

<?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();

總結(jié):this用于實例調(diào)用。self,parent用于調(diào)用靜態(tài)屬性或方法。
最后一個奇特的現(xiàn)象是雖然靜態(tài)調(diào)用父類的非靜態(tài)方法處理數(shù)據(jù),會得到一個報錯,但是還是輸出了int(3)

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板