Jump to content

PHP inherit two classes


c_pattle

Recommended Posts

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. 

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.