c_pattle Posted March 23, 2011 Share Posted March 23, 2011 I have a class that extends my base class as the base class has functions that all classes will need to use. However I have a function in my class that requires an email to be sent and I have a separate class for sending emails. The logical thing to do would be for my class to be an extension of both of these classes. However from what I've read you can do that in PHP so could any recommend as good way to solve this problem? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/ Share on other sites More sharing options...
MrXHellboy Posted March 23, 2011 Share Posted March 23, 2011 i guess you have a method in a parent class right ? Then as example <?php class something{ public function mail(){ echo 'Put your code here'; } } class dosomethingelse extends something{ public function mailto(){ parent::mail(); } } $do = new dosomethingelse; $do->mailto(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191430 Share on other sites More sharing options...
KevinM1 Posted March 23, 2011 Share Posted March 23, 2011 You could (in no particular order): 1. Pass the base/child object as a parameter to the email object's mail function 2. Have the base class implement an email interface if the base class and all of its children need to adhere to that kind of contract 3. Make your email class static, allowing the base/child object to send the email without the need of an email object 4. Use dependency injection to compose one of them into the other - have the email object contain the base/child object (eh, not the way I'd go) or do the opposite, with the base/child object containing the email object (better of the two choices) 5. Do what was suggested above There could be more viable options. Without seeing code, it's hard to say what would be the most elegant solution. Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191501 Share on other sites More sharing options...
trq Posted March 23, 2011 Share Posted March 23, 2011 The logical thing to do would be for my class to be an extension of both of these classes. No it wouldn't. Are the classes at all related or does one simply have some functionality that the other requires? There is a *MASSIVE* difference. Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191525 Share on other sites More sharing options...
c_pattle Posted March 24, 2011 Author Share Posted March 24, 2011 Thanks for all your help. I'll post some code to make it easier to understand what I want to achieve. This is my base class which connects to a database (it will contain more methods in time). class baseClass { public $mysql_conn; function __construct($db) { $this->mysql_conn = $db; $rs = mysql_select_db("webdev_db", $this->mysql_conn->db) or die ('Could not connect: ' . mysql_error()); } } Then I have a class called userClass and in that class is a method that is going to be used to email login details to a user. However because the userClass is already extending the baseClass I can't extend the userClass to inherit the methods in emailClass (my class for handling emails). The emailClass and the userClass aren't related I just need the functionality in that class. Hope that makes sense. class userClass extends baseClass { function __construct($db) { parent::__construct($db); } function emailDetails() { //Code to send email } Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191806 Share on other sites More sharing options...
shlumph Posted March 24, 2011 Share Posted March 24, 2011 You don't need to extend the emailClass to get it's functionality, you can always inject it into the userClass. Or have some sort of email factory and pass it a userClass object. There's a lot of different solutions that involve composition rather than inheritance Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191842 Share on other sites More sharing options...
KevinM1 Posted March 24, 2011 Share Posted March 24, 2011 With OOP, you have to realize that inheritance is: 1. Inflexible 2. Creates is-a relationships When you extend a base class, you're saying "An object of the child class is a object of the base class, too." Your User should not be an email object. This kind of situation is why OOP stresses interfaces and composition. It's easier to say "An object of class X also implements/fulfills interface I" or, "An object of class X contains an object of class Y as a member." I'd simply turn your email class static (an email class is unlikely to retain state, so you're most likely not going to need an object) and use it directly. class Email { public static function UserMail(User $user) { // send user mail } } class User extends Base { public function EmailDetails() { Email::UserMail($this); } } It should work seamlessly if you have your autoload setup correctly. You should really learn some of the theory of OOP before you continue much further. Inheritance is a nice tool to have at times, but it's definitely not the only one, nor is it necessary a lot of the time. There are other, often better ways for objects and classes to interact with each other. Get Matt Zandstra's book on PHP OOP and the Gang of Four's book on the subject. They will fill in a lot of the gaps. Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191847 Share on other sites More sharing options...
ignace Posted March 24, 2011 Share Posted March 24, 2011 Your User should not be an email object. No, but it certainly would be funny! Thank God for not open-sourcing his source code, who knows what I may have done with DNA or Atoms EDIT: I need to get off those meds.. Quote Link to comment https://forums.phpfreaks.com/topic/231522-php-inherit-two-classes/#findComment-1191906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.