[英]Call a method from parent class
我真的為這個問題瘋狂。 我無法在子類的靜態方法中從父類調用方法。
這是我嘗試過的方法,但是不起作用。
class custom extends service {
private $service;
function __construct() {
parent::__construct();
$this->service = new service;
}
public static function activematches($callback) {
$select_by_user = parent::$db->select('matches', '*', array('user_id' => $user_id,
if (count($select_by_user) == 0 && count($select_by_opponent) == 0)
parent::$check->send('11');
else
$this->service->make($callback['request'], $callback['data']);
}
當我調用$this->service
我得到:
Fatal error: Using $this when not in object context
我嘗試將其設置為靜態,嘗試通過調用父方法parent :: method將相同的方法放入子類中,但沒有任何方法...
我是OOP的新手,有什么幫助嗎?
為了在靜態調用中進行訪問,屬性也必須定義為靜態
protected static $services;
從那里,您都需要在靜態方法中進行引用。
self::$services
要么
static::$services
在這種情況下引用self
將引用定義了引用的$ services屬性。 static
將從引用被引用的類上下文中引用屬性。 有關更多信息,請參見手冊中有關后期靜態綁定的說明
更新
基於在這種情況下custom
擴展service
的事實,我懷疑這是您真正追求的。 像這樣的類定義:
class custom extends service {
public function activematches($callback, $user_id) {
$select_by_user = $this->db->select('matches', '*', array('user_id' => $user_id));
if (count($select_by_user) == 0 && count($select_by_opponent) == 0)
$this->check->send('11');
else
$this->make($callback['request'], $callback['data']);
}
}
可能更接近您想要的。
如果父方法的make
不是靜態的:您不能從子類的靜態方法的父類中調用非靜態方法。 您是否考慮過讓子方法不是靜態的? 我認為這是您的最佳選擇。
如果父方法make
是靜態的:
parent::make($callback['request'], $callback['data']);
但這稱為PHP 5.3.0中引入的Late Static Bindings 。 在舊版本中將無法使用,因此請務必小心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.