Connecting Objects in OOP ? How ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

d11wtq wrote:
jmut wrote:
Cameri wrote:.....

Code: Select all

// ... snip ...   
      public function __construct (Address $address=null,Contact $contact=null) {
// ... snip ...
Type hinting is enough to ensure objects are of required type. checking with instanceof is not necessary.
They were already type hinting but then further checking for the actual type of object not just an inherited type. (Sadly) PHP does not support true method overloading so you need to do things like this if you plan on passing mutliple types following a common interface to the same method.

###OFFTOPIC but question is answered more or less###

Not sure what you mean about type hinting. Can you give an example where type hinting will not work with inthereted objects following common interface and instanceof is required?

As for passing null I have to agree..but then again I would consider this poor design (btw in java world is this possible with null..bypassing the object type?).
If address/contact will conditionaly be added then additional methods should be provided for this with no null option. Other option is to include nullable Address object and nullable Contact object which is still object...not null.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Well, for a start null will pass any type-hint. You can (in PHP5) do this:

Code: Select all

class Foo
{
    public function bar(MyClass $obj=null)
    {
        //
    }
}
I also didn't say it doesn't pass the type hint, I was referring more to identifying the exact type.

For example:

Code: Select all

abstract class Mime
{
    //some common functionality
}

class MimePart extends Mime
{
    //Part of a multipart message for email
}

class Attachment extends Mime
{
    //A file attachment for an email
}

class Email
{
    protected $attachments = array();
    protected $parts = array();

    public function add(Mime $obj)
    {
        if ($obj instanceof Attachment) $this->attachments[] = $obj;
        else if ($obj instanceof MimePart) $this->parts[] = $obj;
    }
}
Both MimePart and Attachment are of type "Mime" but in order for us to put them into the correct containers we need to do some further investigation other than them being of type Mime we need to know if we've been given an Attachment or a MimePart.

In some languages you have Method Overloading for this purpose (you can declare the method twice using different params since the method is identified by both it's parameter list and its name).

Code: Select all

public abstract class Mime
{
    //
}

public class MimePart extends Mime
{
    //
}

public class Attachment extends Mime
{
    //
}

public class Email
{
    protected ArrayList parts;
    protected ArrayList attachments;
    
    public Email()
    {
        this.parts = new ArrayList();
        this.attachments = new ArrayList();
    }
    
    //we overload the add() method
    
    public void add(Attachment att)
    {
        this.attachments.add(att);
    }

    public void add(MimePart part)
    {
        this.parts.add(part);
    }
}
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

d11wtq wrote:...

Code: Select all

public abstract class Mime
{
    //
}

public class MimePart extends Mime
{
    //
}

public class Attachment extends Mime
{
    //
}

public class Email
{
    protected ArrayList parts;
    protected ArrayList attachments;
    
    public Email()
    {
        this.parts = new ArrayList();
        this.attachments = new ArrayList();
    }
    
    //we overload the add() method
    
    public void add(Attachment att)
    {
        this.attachments.add(att);
    }

    public void add(MimePart part)
    {
        this.parts.add(part);
    }
}

Nice, I see your point now.
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

Whoaa...thanks a lot guys...

Sorry it took me a while to absorb new things ! I am now still trying to figure it out, and thinking about what you all suggested and what I read from the book...

I'll post it here later...

See you,
Chris
Post Reply