473,387 Members | 1,606 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,387 software developers and data experts.

Polymorphism in PHP

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
13 7854
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
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

"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

"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
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
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
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
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
"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
"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
"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
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
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 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...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.