By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

Polymorphism in PHP (+ question on casting "up")

P: n/a
If I have the following class heirarchy:

class A{
protected $m_type;

function type(){return $this->m_type;}
}

class B extends A{}
class C extends B{}

class D{
private A $m_objref ; //reference to object of type A

//Is this possible?
public function foobar(A $obj){
switch($obj->type()){
case: 1 //treat as A
A $myvar_a = obj;
break;

case: 2 //treat as B (do I need an explicit cast here?)
B $myvar_b = obj;
break;

case: 3 //treat as C (do I need an explicit cast here?)
C $myvar_c = obj;
break;
}
}
}
}

Jun 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ronald Raygun wrote:
If I have the following class heirarchy:

class A{
protected $m_type;

function type(){return $this->m_type;}
}

class B extends A{}
class C extends B{}

class D{
private A $m_objref ; //reference to object of type A

//Is this possible?
public function foobar(A $obj){
switch($obj->type()){
case: 1 //treat as A
A $myvar_a = obj;
break;

case: 2 //treat as B (do I need an explicit cast here?)
B $myvar_b = obj;
break;

case: 3 //treat as C (do I need an explicit cast here?)
C $myvar_c = obj;
break;
}
}
}
}

Maybe a better question would be what are you actually trying to do?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #2

P: n/a


Jerry Stuckle wrote:
Ronald Raygun wrote:
>If I have the following class heirarchy:

class A{
protected $m_type;
function type(){return $this->m_type;}
}

class B extends A{}
class C extends B{}

class D{
private A $m_objref ; //reference to object of type A
//Is this possible?
public function foobar(A $obj){
switch($obj->type()){
case: 1 //treat as A
A $myvar_a = obj;
break;

case: 2 //treat as B (do I need an explicit cast here?)
B $myvar_b = obj;
break;

case: 3 //treat as C (do I need an explicit cast here?)
C $myvar_c = obj;
break;
}
}
}
}


Maybe a better question would be what are you actually trying to do?
I have a session class that stores a user. There are different types of
users (with different methods), but they each derive from a base User
class. I want to have one single reference that points to the user object.

The case I make above helps to find out what the PHP language limits are
(not to mention "gotchas", if I make C++ like assumptions in my code).

On a more practical level, on pages where a user (of type B for example)
is expected, (after preliminary sanity checks), I will need to start
treating the variable as a variable of Type B, although it is stored as
a reference to a User (remember Class B is-a 'User'). This is where my
question about "casting down[corrected]" (the class diagram) comes in.

Can I simple sstart calling methods on the 'B' interface, or do I need
to make an explicit cast from 'User' to 'B', before using the object
retrieved from the session?
Jun 2 '08 #3

P: n/a
Ronald Raygun wrote:
>

Jerry Stuckle wrote:
>Ronald Raygun wrote:
>>If I have the following class heirarchy:

class A{
protected $m_type;
function type(){return $this->m_type;}
}

class B extends A{}
class C extends B{}

class D{
private A $m_objref ; //reference to object of type A
//Is this possible?
public function foobar(A $obj){
switch($obj->type()){
case: 1 //treat as A
A $myvar_a = obj;
break;

case: 2 //treat as B (do I need an explicit cast here?)
B $myvar_b = obj;
break;

case: 3 //treat as C (do I need an explicit cast here?)
C $myvar_c = obj;
break;
}
}
}
}


Maybe a better question would be what are you actually trying to do?

I have a session class that stores a user. There are different types of
users (with different methods), but they each derive from a base User
class. I want to have one single reference that points to the user object.

The case I make above helps to find out what the PHP language limits are
(not to mention "gotchas", if I make C++ like assumptions in my code).

On a more practical level, on pages where a user (of type B for example)
is expected, (after preliminary sanity checks), I will need to start
treating the variable as a variable of Type B, although it is stored as
a reference to a User (remember Class B is-a 'User'). This is where my
question about "casting down[corrected]" (the class diagram) comes in.

Can I simple sstart calling methods on the 'B' interface, or do I need
to make an explicit cast from 'User' to 'B', before using the object
retrieved from the session?
OK, no casting is needed in PHP. But IMHO this isn't a good OO design.
If you're going to be calling methods depending on the type, those
methods should be defined in the base class with a default response and
overridden as necessary in the derived classes.

The same is true in C++, BTW. Down casting is highly frowned upon. It
creates too many dependencies in the code and makes the code difficult
to maintain. Things OO was designed to make better.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #4

P: n/a
<snip>
Can I simple sstart calling methods on the 'B' interface, or do I need
to make an explicit cast from 'User' to 'B', before using the object
retrieved from the session?
If you really want to check if the instance if of class/interface B, use
the instanceof operator. But if you already know it is of
class/interface B, you can just call the method.

If you are used to Visual Basic, this may be surprising to you. But an
interface in PHP just says "conforming objects are guaranteed to have
these methods". Interfaces in PHP do not hide or rename methods.
Jun 2 '08 #5

P: n/a
On May 15, 2:51 pm, Dikkie Dik <dik...@nospam.orgwrote:
<snip>
Can I simple sstart calling methods on the 'B' interface, or do I need
to make an explicit cast from 'User' to 'B', before using the object
retrieved from the session?

If you really want to check if the instance if of class/interface B, use
the instanceof operator. But if you already know it is of
class/interface B, you can just call the method.

If you are used to Visual Basic, this may be surprising to you. But an
interface in PHP just says "conforming objects are guaranteed to have
these methods". Interfaces in PHP do not hide or rename methods.
To function correctly, the code only requires at least part of the
interface to be present - instanceof will allow the coder to check if
the object is descended from a class which implements what might be a
suitable method / member variable but its really just a matter of
convenience to have common ancestry - even Java allows interfaces to
be declared independently of the classes which implement them. Long
ancestries can be a performance bottleneck in PHP.

Sometimes method_exists() or property_exists() will be a better way to
work out how to interact with an object.

C.
Jun 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.