Page 2 of 2

Posted: Sat Nov 04, 2006 1:00 pm
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.

Posted: Sat Nov 04, 2006 2:11 pm
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);
    }
}

Posted: Sat Nov 04, 2006 2:37 pm
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.

Posted: Thu Nov 09, 2006 12:21 am
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