Page 1 of 1
Multiple inheritance
Posted: Tue Oct 21, 2008 2:55 am
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
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 5:45 am
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.
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 6:26 am
by papa
My apologies.
Thanks for the answer.
edit.
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 10:13 am
by pickle
VladSun wrote:Also, you are not following the MVC.
That's perfectly alright. MVC is
not the end-all solution for everything.
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 10:51 am
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.
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 12:58 pm
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.
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 1:17 pm
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");
Re: Multiple inheritance
Posted: Tue Oct 21, 2008 1:22 pm
by papa
Nice!
Really apreciate the help!
I will go for your suggestion, looks good.