php中static和self的區別

2022-08-13 11:51:14 字數 621 閱讀 6910

在閱讀一些框架的原始碼時發現了new static(),和new self(),甚是不解,後來查閱資料,才知道static採用了延遲繫結,能準確知道是父類還是子類的呼叫。這就是說static是個聰明的小孩,家裡的親戚的輩分他都能準確的叫出;而不是像他的兄弟self,只知道自己的親爹媽。

例子如下:

<?php

class father

public static function whoami_static()

public static function getinstance_self()

public static function getinstance_static()

}class fatherbrother extends father

fatherbrother::whoami_self(); //father

fatherbrother::whoami_static(); //uncle

var_dump(fatherbrother::getinstance_self()); //father

var_dump(var_dump(fatherbrother::getinstance_static())); //uncle

php中static和self的區別

在閱讀一些框架的原始碼時發現了new static 和new self 甚是不解,後來查閱資料,才知道static採用了延遲繫結,能準確知道是父類還是子類的呼叫。這就是說static是個聰明的小孩,家裡的親戚的輩分他都能準確的叫出 而不是像他的兄弟self,只知道自己的親爹媽。例子如下 class ...

PHP 中 static 和 self 的區別

使用 self 或者 class 對當前類的靜態引用,取決於定義當前方法所在的類 使用static 不再被解析為定義當前方法所在的類,而是在實際執行時計算的。也可以稱之為 靜態繫結 因為它可以用於 但不限於 靜態方法的呼叫。靜態繫結是php 5.3.0,增加的乙個功能 用於在繼承範圍內引用靜態呼叫的...

php物件導向中self和static的區別

在php的物件導向程式設計中,總會遇到 class test public static function func 可你知道self和static的區別麼?其實區別很簡單,只需要寫幾個demo就能懂 class car protected static function getmodel car ...