从子类初始化父类变量

时间:2021-02-27 16:08:20

I have 2 classes: one is an DBobject which contains general database query and the other child classes that each has a specific database query. Now the problem is when I try to initialize the parent class table_name variable it is not being initialized. I have tried many ways but it shows that the table is empty when the query is performed. Here's the code:

我有两个类:一个是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";
}
?>


?>

The main application:

主要应用:

<?php
$cata = Catagory::find_all();
     foreach($cata as $catag){
     echo "Catagory Name :" . $catag->name."<br/>";
} 


 ?>

I want to be able to initialize the type of the table that the DBobject function find_all() will work on. Please help me with this. This is the error that I get. Noticet that the query does not contain the name of the table.

我希望能够初始化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 

1 个解决方案

#1


0  

If you are on php 5.3 or above, you can do this because they added 'late static bindings'

如果您使用的是php 5.3或以上版本,您可以这样做,因为它们添加了“后期静态绑定”

http://php.net/manual/en/language.oop5.late-static-bindings.php

http://php.net/manual/en/language.oop5.late-static-bindings.php

you can use get_called_class() or static:: (instead of self::) to reference the class that was CALLED instead of the class the code is in

您可以使用get_called_class()或static::(而不是self::)来引用被调用的类,而不是代码所在的类

#1


0  

If you are on php 5.3 or above, you can do this because they added 'late static bindings'

如果您使用的是php 5.3或以上版本,您可以这样做,因为它们添加了“后期静态绑定”

http://php.net/manual/en/language.oop5.late-static-bindings.php

http://php.net/manual/en/language.oop5.late-static-bindings.php

you can use get_called_class() or static:: (instead of self::) to reference the class that was CALLED instead of the class the code is in

您可以使用get_called_class()或static::(而不是self::)来引用被调用的类,而不是代码所在的类