Multiple inheritance

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Multiple inheritance

Post by papa »

Hi,

How do I get around the limitation of nothing being able to multi inherit?

I have a class called MySQLConnect. My class Admin inherit the db connection, mysql queries. I have another class called Form that deals with html forms. How do I incorporate my form functions to Admin and still being able to use my mysql functions?

I found this, but not that keen of making additional classes etc...

http://www.sitepoint.com/article/dual-i ... ses-php/2/

Any design ideas or code help?

thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Multiple inheritance

Post by VladSun »

I don't think your DB class and Admin/Form classses have any "is-a" relationship so you don't need inheritance here.
Also, you are not following the MVC.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Multiple inheritance

Post by papa »

My apologies.

Thanks for the answer.

edit.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multiple inheritance

Post by pickle »

VladSun wrote:Also, you are not following the MVC.
That's perfectly alright. MVC is not the end-all solution for everything.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Multiple inheritance

Post by allspiritseve »

papa wrote:How do I get around the limitation of nothing being able to multi inherit?
That limitation is a good thing. If you feel like you need to extend from more than one class, that's a good sign that you need to be using composition, not inheritance.

As VladSun pointed out, an admin class isn't a database connection, and it isn't a form handling class. Rather, your admin class should be using those classes through composition. A simple way to use them in your admin class is to pass them into the constructor:

Code: Select all

class Admin {
 
function __construct ($db, $form)   {
    $this->db = $db;
    $this->form = $form;
}
 
function sampleMethod() {
    $this->db->query ('SELECT * FROM ...');
    $ths->form->addTextField ('key', 'value'); // Or whatever your form class does
}
 
}
Then any method in your class can use them by calling "$this->db" or "$this->form".

Edit: Also, please note that the article you linked to was written in 2002, and is very outdated.
Last edited by allspiritseve on Tue Oct 21, 2008 1:09 pm, edited 1 time in total.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Multiple inheritance

Post by papa »

I realised that after reading your input and reviewing my code, that I don't need to inherit twice.

The Form class is going to work perfectly by itself, however the idea with my Admin class is to validate the data and then insert the user input in the db.

Example from Admin:

Code: Select all

 
    function addProject() {
        $f = new Form("post", "index.php?a=new_project_submit", "", "");
        $genre["Genre"] = $this->mySQLselect("id, name", "genre", "name");
        echo $f->formStart();
        echo "<p>";
        echo $f->formLabel("project_name", "Name");
        echo $f->formText("project_name", "", "", "", "text")."</p>\n";
        echo "<p>";
        echo $f->formLabel("project_img", "Image");
        //echo $this->formUpload("project_img", "", "text")."</p>\n";
        echo $f->formText("project_img", "", "", "", "text")."</p>\n";
        echo "<p>";
        echo $f->formLabel("project_descr", "Description");
        echo $f->formTextarea("project_descr")."</p>\n";
        echo "<p>";
        echo $f->formLabel("project_genre", "Genre");
        echo $f->formSelect("project_genre", $genre)."</p>\n";
        echo "<p>";
        echo $f->formLabel("", "");
        echo $f->formSubmit("", "Add Project", "", "button")."</p>\n";
        echo $f->formEnd(); 
    }
Not implemented yet is the function that validates and inserts input. Looking at the code know makes me wonder if I should get rid of the inheritance completely for both classes...

For example $genre is getting genres from the db which could probably be solved without inherit mySQLConnect...

Don't know if my design is that good, but always open for input.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Multiple inheritance

Post by allspiritseve »

papa wrote: $f = new Form("post", "index.php?a=new_project_submit", "", "");
$genre["Genre"] = $this->mySQLselect("id, name", "genre", "name");
I see your form class doesn't need to be passed around. That looks good.

A database class is definitely something you want shared though. You don't want every class that needs it to have to inherit from it, and you don't want to create a new connection for every class that needs it. If you create the connection and store it in a variable ($db), you can pass that into the constructor and do this instead:

Code: Select all

$genre["Genre"] = $this->db->mySQLselect("id, name", "genre", "name");
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Multiple inheritance

Post by papa »

Nice!

Really apreciate the help!

I will go for your suggestion, looks good.
Post Reply