12

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.

TheLettuceMaster
  • 14,856
  • 42
  • 142
  • 248
  • I recommend that you read more about OOP and especially the way it's implemented in PHP. Giving you a working code won't solve the real problem. – Adi Jun 25 '12 at 17:49
  • 23
    On the contrary, I learn by other people's code. Perhaps I will wait for someone to actually answer the question now. – TheLettuceMaster Jun 25 '12 at 17:52
  • Ive read "I learn from other peoples code" too often to know, that it doesn't work well. You should consider reading a book. – KingCrunch Jun 25 '12 at 17:58
  • 1
    Linkspam: [one](http://www.php.net/manual/en/language.oop5.basic.php), [two](http://www.php.net/manual/en/language.oop5.inheritance.php), [three](http://php.net/manual/en/keyword.parent.php), [four](http://stackoverflow.com/questions/6456939/php-accessing-parent-class-variable) – jedwards Jun 25 '12 at 17:59
  • But how do expect to learn theory concepts by skimming a code? Do you realize that you're trying to access a child property from a parent? You're not organizing your methods according to their relation to the class. Trust me, we all want to help you here, but nobody is gonna write a book for you as an answer. Just relax, read a book. If not, then read some PHP OOP tutorial. – Adi Jun 25 '12 at 18:03
  • Fair enough. I assumed I knew enough of the concept where it was a tweak here, or a tweak there. If not, then I will attempt a different way or just stick with my old duplicate coding way for the moment. However, You'd be surprised by what I have accomplished looking at other's codes. – TheLettuceMaster Jun 25 '12 at 18:35
  • I apologize for this topic, you guys were 100%, after further review, this was way over my head. Had I known what I was asking--I would not have asked. I really did think it was a simple tweak here and there. For what its worth, I am actually reading a fairly intensive OO Java book and it is very similar to the concepts here. But I am still far from mastering it. – TheLettuceMaster Jun 26 '12 at 15:30
  • 4
    I think there was nothing wrong with the question, just not no one had an answer. But thanks to @Matthijs we are enlightened – Ibu Jul 12 '12 at 18:15
  • You can try to describe a picture with a thousand words, or you can just show the damn picture! – akinuri Jul 17 '18 at 06:45

2 Answers2

30

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();
Martin
  • 19,815
  • 6
  • 53
  • 104
Matthijs
  • 405
  • 3
  • 6
5

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.