Multiple inheritance
Moderator: General Moderators
Multiple inheritance
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
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
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.
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
Re: Multiple inheritance
My apologies.
Thanks for the answer.
edit.
Thanks for the answer.
edit.
Re: Multiple inheritance
That's perfectly alright. MVC is not the end-all solution for everything.VladSun wrote:Also, you are not following the MVC.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Multiple inheritance
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.papa wrote:How do I get around the limitation of nothing being able to multi inherit?
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
}
}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.
Re: Multiple inheritance
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:
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.
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();
}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.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Multiple inheritance
I see your form class doesn't need to be passed around. That looks good.papa wrote: $f = new Form("post", "index.php?a=new_project_submit", "", "");
$genre["Genre"] = $this->mySQLselect("id, name", "genre", "name");
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
Nice!
Really apreciate the help!
I will go for your suggestion, looks good.
Really apreciate the help!
I will go for your suggestion, looks good.