Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Extending Class

I've done lots and lots of code in PHP that is object-oriented, but up until now, all of my classes have been, "singular", I suppose you can call it. I am in the process of changing several classes (that have 5 or so identical methods) to extend one class (to rid myself of duplicate code). I am running into a few issues.

I am trying to access a method in a parent class, but you can see the issue.

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;

  }

Code trying to access all info from this table from 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 merged to the new way of doing things. I need to change the self::$table_name which no longer works in the new way I am doing things. I will have about 5 Classes extending this object, so what is the best way of coding this so I can access different tables with one method (rather than including this exact find_all() method in 5 different classes.

like image 204
TheLettuceMaster Avatar asked Jun 25 '12 17:06

TheLettuceMaster


People also ask

What is extending class in PHP?

The extends keyword is used to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.

Can you extend 2 classes in PHP?

Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.

What is an Extend class?

Definition and Usage The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Can a trait extend a class PHP?

Only classes can be extended. Traits are not classes. They can be used/included by classes but are not classes themselves.


2 Answers

You could try late static binding as mentioned below, or a singleton solution should work as well:

<?php
abstract class DatabaseObject {
  private $table;
  private $fields;

  protected function __construct($table, $fields) {
    $this->table = $table;
    $this->fields = $fields;
  }

  public function find_all() {
    return $this->find_by_sql('SELECT * FROM ' . $this->table);
  }
}

class Topics extends DatabaseObject {
  private static $instance;

  public static function get_instance() {
    if (!isset(self::$instance)) {
      self::$instance = new Topics('master_cat', array('cat_id', 'category'));
    }

    return self::$instance;
  }
}

Topics::get_instance()->find_all();
like image 73
Matthijs Avatar answered Sep 23 '22 00:09

Matthijs


You should look into the concept of Late Static Binding in PHP. This allows you to access a static constant or function from the class that was called.

like image 5
Jeremy Livingston Avatar answered Sep 24 '22 00:09

Jeremy Livingston