[英]Initializing Parent class variable from the child class
我有2個類:一個是包含常規數據庫查詢的DBobject,另一個是每個都有特定數據庫查詢的子類。 現在的問題是,當我嘗試初始化父類table_name變量時,它尚未初始化。 我嘗試了很多方法,但是顯示執行查詢時表是空的。 這是代碼:
<?php
class DBobject {
public static $table_name="";
public static function find_all(){
global $database;
return self::find_by_sql("SELECT * FROM ".self::$table_name);
}
}
?>
<?php
class Catagory extends DBobject{
public static $table_name ="catagory";
}
?>
?>
主要應用:
<?php
$cata = Catagory::find_all();
foreach($cata as $catag){
echo "Catagory Name :" . $catag->name."<br/>";
}
?>
我希望能夠初始化DBobject函數find_all()可以使用的表的類型。 請幫我解決一下這個。 這是我得到的錯誤。 注意,查詢不包含表的名稱。
Database query Failed You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
last SQL query: SELECT * FROM
如果您使用的是php 5.3或更高版本,則可以這樣做,因為它們添加了“后期靜態綁定”
http://php.net/manual/en/language.oop5.late-static-bindings.php
您可以使用get_ Called_class()或static::(而不是self::)來引用已調用的類,而不是代碼所在的類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.