[請益] 取得 static method 實際上的 caller class type.

看板PHP作者 (墳墓)時間18年前 (2007/05/11 13:35), 編輯推噓2(201)
留言3則, 1人參與, 最新討論串1/1
假設有以下的父類別: class Father { public static function temp () { //印出實際上呼叫這個 static method 的子類別 //echo __CLASS__; //echo get_class(); } } 以及以下的子類別: class Child extends Father{ } class AnotherChild extends Father { } 主程式: Child::temp(); AnotherChild::temp(); 我想做的是,想在呼叫 Child::temp() 時會印出 Child,若是呼叫 AnotherChild::temp() 會印出 AnotherChild。 可是目前我所試的方法,都只能印出 Father 而已,不曉得有沒有什 麼比較恰當的方法? BTW,在 temp 是 object method 的時候我已經試出來了,直接用 get_class() 就可以,不過因為在我的實作裡,temp 比較偏向 class method 而不是 object method,所以這個方法不是很適用。 -- ~ 白馬帶著她一步步地回到中原。白馬已經老了,只能慢慢地走, 'v' Brian Hsu 但終是能回到中原的。江南有楊柳、桃花,有燕子、金魚…… // \\ ( 墳 墓 ) /( )\ 但這個美麗的姑娘就像古高昌國人那樣固執。 【白馬嘯西風】 ^`~'^ http://bone.twbbs.org.tw/blog 『那都是很好很好的,可我偏不喜歡。』 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.22.18.90

05/11 13:52, , 1F
我覺得這邊用 static function 有點詭異?
05/11 13:52, 1F

05/11 14:04, , 2F
我想這邊在 sub-class 用 overwriting 的方式比較適合
05/11 14:04, 2F

05/11 14:07, , 3F
^^^^^^^^^^^ overriding
05/11 14:07, 3F
文章代碼(AID): #16H02Ux9 (PHP)
文章代碼(AID): #16H02Ux9 (PHP)