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

Polymorphism in PHP

P: n/a
Hello! I am working with PHP and MySQL in a proyect with some partners
of our university, and we are thinking about building some classes and
use inheritance and polymorphism.

The problem is that we are not sure of PHP implementing polymorphism
and we couldn't find info about it. I will really appreciate if
someone could give us some recommendations about some books to read
about it and/or pages to look for some info.

Thanks for all.

PD: I am from Argentina, so my real language is Spanish. I am
effective reading in English, but my writing has to be improved. =P
Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Hi,

On 20 Apr 2004 18:42:55 -0700, ga**********@hotmail.com (Gurtaj)
wrote:
Hello! I am working with PHP and MySQL in a proyect with some partners
of our university, and we are thinking about building some classes and
use inheritance and polymorphism.

The problem is that we are not sure of PHP implementing polymorphism
and we couldn't find info about it. I will really appreciate if
someone could give us some recommendations about some books to read
about it and/or pages to look for some info.
Try www.php.net, it explains how the classes work quite well.

Example Polymorphism:

class doer{

function do();

}

class do_b extends doer{

function do(){
print "I'm doing b";
}
}

class do_c extends doer{

function do(){
print "I'm doing c";
}
}
$sample_doer = get_a_sample_doer_somehow();

$sample_doer->do();

does either nothing (class doer), "I'm doing b", "I'm doing c",
depending on what you instantiated.

What PHP4 doesn;t have is declarations like abstract, public, private,
you have to cover that by proper coding and debugging mechanisms.

Also redefining constructors doesn;t work, which is a bit of a pain.

HTH, Jochen



Thanks for all.

PD: I am from Argentina, so my real language is Spanish. I am
effective reading in English, but my writing has to be improved. =P


--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #2

P: n/a
Jochen Daum wrote:
Also redefining constructors doesn;t work, which is a bit of a pain.


What do you mean by that? You can define constructor for a class, and then
another for a subclass, if that's what you meant.

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
Jul 17 '05 #3

P: n/a

"Gurtaj" <ga**********@hotmail.com> wrote in message
news:57**************************@posting.google.c om...
Hello! I am working with PHP and MySQL in a proyect with some partners
of our university, and we are thinking about building some classes and
use inheritance and polymorphism.
PHP 4 has functions that allow for the creation of classes with properties
amd methods, subclassing etc so there should not be a problem. There is no
such thing as a particular function or command that allows for polymorhism -
it is all in the way you write your code.

Take a look at http://www.tonymarston.co.uk/php-mys...seobjects.html
and http://www.tonymarston.co.uk/php-mys...eobjects2.html which
describe how I have implemented a table class which I use to access all my
database tables.

There is also http://www.tonymarston.co.uk/php-mys...d-bad-oop.html which
handles some of the criticism of my approach.

--
Tony Marston

http://www.tonymarston.net
The problem is that we are not sure of PHP implementing polymorphism
and we couldn't find info about it. I will really appreciate if
someone could give us some recommendations about some books to read
about it and/or pages to look for some info.

Thanks for all.

PD: I am from Argentina, so my real language is Spanish. I am
effective reading in English, but my writing has to be improved. =P

Jul 17 '05 #4

P: n/a

"Gurtaj" <ga**********@hotmail.com> wrote in message
news:57**************************@posting.google.c om...
Hello! I am working with PHP and MySQL in a proyect with some partners
of our university, and we are thinking about building some classes and
use inheritance and polymorphism.


OK, I see you have the solution. But what is it that you're trying to solve?

Imagine walking into a doctor's office and being greeted by "we're
thininking about a triple bypass operation and a few electroshock sessions."
Jul 17 '05 #5

P: n/a
Hi,

On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
<be************@dimedia.hr> wrote:
Jochen Daum wrote:
Also redefining constructors doesn;t work, which is a bit of a pain.
What do you mean by that? You can define constructor for a class, and then
another for a subclass, if that's what you meant.


Should have made myself more clear.

What doesn't work, is that you cannot return a subclass object from a
constructor.

I wanted to use that to implement a Strategy pattern (see Book Design
Patterns by Booch etc), which goes roughly like that:

class shape{

function shape($type){

switch ($type){
case "circle":
$x = new circle();
break;
...
}
return $x;
}

function draw(){
print "abstract here";
}
}

Assuming there are different shapes as subclasses of shape.

Shape does always return a class shape and will always use the draw
function of shape, if you go:

$s = new shape("circle");
$s->draw();
HTH, Jochen
Berislav


--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #6

P: n/a
Jochen Daum <jo*********@cabletalk.co.nz> wrote:
What doesn't work, is that you cannot return a subclass object from a
constructor.

I wanted to use that to implement a Strategy pattern (see Book Design
Patterns by Booch etc), which goes roughly like that:

class shape{

function shape($type){

switch ($type){
case "circle":
$x = new circle();
break;
...
}
return $x;
}

function draw(){
print "abstract here";
}
}


I think you can "kind of" get that type of functionality via a so-called
"object factory". (That would could have the advantage of loading a PHP
document implementing the desired class on demand) Unfortunately it's
not a clean subclass.

Jamie
--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX

Jul 17 '05 #7

P: n/a
Gurtaj <ga**********@hotmail.com> wrote:
Hello! I am working with PHP and MySQL in a proyect with some partners
of our university, and we are thinking about building some classes and
use inheritance and polymorphism.

The problem is that we are not sure of PHP implementing polymorphism
and we couldn't find info about it. I will really appreciate if
someone could give us some recommendations about some books to read
about it and/or pages to look for some info.

Thanks for all.

PD: I am from Argentina, so my real language is Spanish. I am
effective reading in English, but my writing has to be improved. =P


As far as I can tell, PHP really doesn't have that feature:

// This won't work.
function myFunction(long a){ ... }
function myFunction(float a) { ... }

This is a side effect of loosely typed languages. To implement similiar
functionality you'd probably need to write PHP code to determine if $a
is a long or a float, then do the right thing.

The extends and overriding methods DOES work though. PHP's version
of java's 'super' is 'parent'.

I've got a class file at:

http://www.geniegate.com/other/paypal/

Specifically designed to be used in this manner. (Override the
interesting methods to do whatever you want)

Jamie

--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX

Jul 17 '05 #8

P: n/a
With total disregard for any kind of safety measures
no****@geniegate.com leapt forth and uttered:
As far as I can tell, PHP really doesn't have that feature:

Polymorphism isn't a "feature", it's a result of code design. And
static typing is *not* a dependancy of polymorphism. Many languages
with much greater object-orientation than PHP use dynamic typing.
(Smalltalk anyone?)
// This won't work.
function myFunction(long a){ ... }
function myFunction(float a) { ... }


Thats won't work because what you're describing is not polymorphism
but "object overloading". Whereby the type passed to a method
determines the method used. This is largely unnecessary in PHP as
PHP allows default parameter values wheras Java does not.

Personally I've never come across a single scenario where I thought
to myself "I wish I could use method overloading here".
--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
Jul 17 '05 #9

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<Hd********************@comcast.com>...
OK, I see you have the solution. But what is it that you're trying to solve?

Imagine walking into a doctor's office and being greeted by "we're
thininking about a triple bypass operation and a few electroshock sessions."


What we were thinking was to implement a classes hierarchy that
allowed us to use just one operation (defined in the ancestor) to save
any class into the data base. Here is where polymorphism would get in
the game.

But we were thinking about it and decided yesterday to implement the
classes in the hierarchy with an operation that allow the object save
itself into the data base.

One more time, thanks for the help and excuse my english.
Jul 17 '05 #10

P: n/a
"Jochen Daum" <jo*********@cabletalk.co.nz> wrote in message
news:qh********************************@4ax.com...
Hi,

On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
<be************@dimedia.hr> wrote:
Jochen Daum wrote:
Also redefining constructors doesn;t work, which is a bit of a pain.


What do you mean by that? You can define constructor for a class, and thenanother for a subclass, if that's what you meant.


Should have made myself more clear.

What doesn't work, is that you cannot return a subclass object from a
constructor.


Sure ya can. I do it all the time :-)

Watcha this...

class shape{

function shape($type){

switch ($type){
case "circle":
$this = new circle();
break;
...
}
return;
}

Ok, ok, I did not technically "return" the value - but it accomplishes what
you wanted to do.
Jul 17 '05 #11

P: n/a
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> wrote in message
news:ws*******************@nwrddc01.gnilink.net...
"Jochen Daum" <jo*********@cabletalk.co.nz> wrote in message
news:qh********************************@4ax.com...
Hi,

On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
<be************@dimedia.hr> wrote:
Jochen Daum wrote:
> Also redefining constructors doesn;t work, which is a bit of a pain.

What do you mean by that? You can define constructor for a class, and thenanother for a subclass, if that's what you meant.
Should have made myself more clear.

What doesn't work, is that you cannot return a subclass object from a
constructor.


Sure ya can. I do it all the time :-)

Watcha this...

class shape{

function shape($type){

switch ($type){
case "circle":
$this = new circle();
break;
...
}
return;
}

Ok, ok, I did not technically "return" the value - but it accomplishes

what you wanted to do.


Why not just do $object = new $shape(); ? It's perfectly clear what you're
trying to accomplish. Bid and switch constructors are immoral if you ask me
:-)
Jul 17 '05 #12

P: n/a
Hi Chung,

On Thu, 22 Apr 2004 19:21:32 -0400, "Chung Leong"
<ch***********@hotmail.com> wrote:
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> wrote in message
news:ws*******************@nwrddc01.gnilink.net.. .
"Jochen Daum" <jo*********@cabletalk.co.nz> wrote in message
news:qh********************************@4ax.com...
> Hi,
>
> On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
> <be************@dimedia.hr> wrote:
>
> >Jochen Daum wrote:
> >> Also redefining constructors doesn;t work, which is a bit of a pain.
> >
> >What do you mean by that? You can define constructor for a class, and then
> >another for a subclass, if that's what you meant.
>
> Should have made myself more clear.
>
> What doesn't work, is that you cannot return a subclass object from a
> constructor.


Sure ya can. I do it all the time :-)

Watcha this...

class shape{

function shape($type){

switch ($type){
case "circle":
$this = new circle();
break;
...
}
return;
}

Ok, ok, I did not technically "return" the value - but it accomplishes

what
you wanted to do.


Why not just do $object = new $shape(); ? It's perfectly clear what you're
trying to accomplish. Bid and switch constructors are immoral if you ask me

With a strategy pattern you want to "delegate" the decision of the
switch. Of course you can use an array instead or whatever.

HTH, Jochen:-)


--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #13

P: n/a
Phil Roberts schrieb:
Thats won't work because what you're describing is not polymorphism
but "object overloading". Whereby the type passed to a method
determines the method used. This is largely unnecessary in PHP as
PHP allows default parameter values wheras Java does not.

Personally I've never come across a single scenario where I thought
to myself "I wish I could use method overloading here".


PHP doesn't support default parameters for reference type parameters. I
wanted to do this

function huba(&$relay=none)
{
//...
if($relay=none)
{
// special code if no relay object specified
}
//...
}

This is not possible. You MUST specify reference type parameters.
AllOLLi
PS: Wikipedia:
In computer science, polymorphism is the idea of allowing the same code
to be used with different types, resulting in more general and abstract
implementations.
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.