0

Probably a silly question, I have many classes in PHP with this template:

class SomeTable {
    private $db;
    private $prefix;
    private $tbl_name;

    function __construct($db, $tbl_name) {
            $this->db = $db;
            $this->prefix = $db->tblPrefix();
            $this->tbl_name = $tbl_name;
            $this->install();
    }

    private function install() {
        $sql = "CREATE TABLE IF NOT EXISTS...";
        $this->db->query($sql, null);
    }

    // query functions
    ...
}
  • I have one of these classes to represent each table in the database.
  • The 3 private variables at the top must be available in each class, and they are set in the constructor.
  • The constructor is the same for every class.
  • The install() function is different for every class because it is used to create the database table. (I like to keep it here because I can keep looking back at it while I'm writing/editing queries).

Do I then create an abstract class with the 3 variables, constructor, empty install() function and implement it in each class? Will it work the same for me?

I'm sorry if it's a dumb question, I've really never used classes/inheritance before in PHP.

3
  • Implementing is different then Inheriting. Implements means you must define the install method everywhere that class is implemented, as the needed functionality could vary. Inherits means you have access to call the already coded class, and use those variables, as the code for your install method is always the same. Commented Aug 27, 2013 at 21:33
  • 2
    An abstract base class is normally a well fitting container for template code, yes. This is not totally stupid. It's probably worth to know that you can make some methods final and some others abstract. Commented Aug 27, 2013 at 21:34
  • Thanks, I guess I was a little afraid to try it. I also wondered with PHP not being a real OOP language if I'd have many issues using this approach. Commented Aug 27, 2013 at 21:38

2 Answers 2

1

Do I then create an abstract class with the 3 variables, constructor, empty install() function and implement it in each class? Will it work the same for me?

Yes. The 3 variables will have to be protected now though, instead of private, since they need to be access from child classes. Something like this:

abstract class DBTable{
    protected $db;
    protected $prefix;
    protected $tbl_name;

    function __construct($db, $tbl_name) {
            $this->db = $db;
            $this->prefix = $db->tblPrefix();
            $this->tbl_name = $tbl_name;
    }

    abstract function install();
}

class SomeTable extends DBTable {
    private function install() {
        $sql = "CREATE TABLE IF NOT EXISTS...";
        $this->db->query($sql, null);
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this... I probably would have done exactly the same as above except not using the protected key and then failing before reverting :).
1

If you extend a class you will inherit any public or protected methods that exist in the parent class. So if you have a class like so:

class Database {

    public function get_database(){
        return 'blah';
    }

}

And then you extend the class like so:

class Blah extends Database {
    ...
}

Now, the Blah class will inherit the method get_database() so both Database::get_database() and Blah::get_database() will return the same thing. You can also use the parent:: special name to target the extended class. Note: private methods are only available in the class in which they are instantiated and are not inherited.

I should also note that you can override methods by using the same method name in the child class. So if you write a get_database() method within the Blah class, it will take precedence over the parent's method of the same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.