I have done a lot and a lot of code in PHP, object oriented, but so far all my classes have been โsingleโ, I suppose you can call it that. I am in the process of changing several classes (which have approximately 5 of the same methods) to extend one class (to get rid of code duplication). I am facing a few issues.
I am trying to access the method in the parent class, but you can see the problem.
Parent class:
class DatabaseObject { public static function find_all() { return self::find_by_sql("SELECT * FROM " . self::$table_name); } }
Child class:
class Topics extends DatabaseObject { protected static $table_name = "master_cat"; protected static $db_fields = array('cat_id', 'category'); public $cat_id; public $category; }
The code tries to access all the information from this table from the php / html file:
$topics=Topics::find_all(); foreach($topics as $topic): echo $topic->category; endforeach;
As you can see, most of the code has not been combined with a new way of doing things. I need to change self :: $ table_name, which no longer works in a new way, I am doing something. I will have about 5 classes extending this object, so the best way to encode this so that I can access different tables using one method (instead of including this exact find_all () method in 5 different classes.
php class extends object-oriented-database
Kickinglettuce
source share