473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
5 2172
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


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
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
3
by: Student911 | last post by:
Hello, I read that upward casting is always safe but downward casting/ For example: Derived* p=(Derived*) new Base(); casting can cause problems with the memory. Can someone please give me...
2
by: Frazer | last post by:
hi, I am confused when to use (int) and when to use Convert.toint32.. eg here string s= "1"; int j = (int) s; //this gives me an error and i have to use convert.toint32. why is that so and...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
8
by: Michael | last post by:
Hi, I think my problem deals with class casting and inheritance. I want to deal with various Audio Formats, reading into memory for modification, working with it (done by different classes),...
3
by: lorenzon | last post by:
I've run into a problem in some code involving two class hierarchies that I can't figure out: I have an event hierachy topped with an interface, let's say- IEvent, EventA : IEvent, EventB :...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
1
by: alireza6485 | last post by:
Hi, can u help me with polymorphism and up-casting please. Bipeds scream birds fly bird bd =new bied; Bipeds bp bp=bd(upcasting)//what does this line do?is it saying that the biped we have is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.