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!
// ... 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.
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).
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);
}
}
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);
}
}
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...