Polymorphism

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Jenk wrote:lest not forget func_get_args() etc. too.
THAT'S the function I've been looking for! I thought it'd be fun to program an SQL query function similar to printf(), and have it automatically escape variables. :-D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jenk wrote:lest not forget func_get_args() etc. too.
Oh yeah, and that's only the start of it. You can forget things like function foo (int bar) and function foo (float bar) as well.
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

superdezign wrote:And, out of curiosity, is this a typo? I opened the book and it's the first thing that I read on the "cheat sheet" and I just thought to myself, "Either I'm wrong, or this book may not have been the best choice."
PHP 5 For Dummies wrote:Using empty brackets:

Code: Select all

$colors[]    = "red";
$colors[]    = "blue";
Result:

Code: Select all

$colors[1]   = "red";
$colors[2]   = "blue";
I could have sworn that this code would start the array off at the zero index, but when I think about it, I've grown used to using the foreach loop, so I've never checked.
Yes, PHP arrays are zero-based. So I would suspect that is a type unless somewhere in there there was another array member before that which had a 0 index assigned to it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Thought so! What a wonderful way to introduce me to the book.... And at no point in the cheat sheet does it mention that the first index of an array is zero.

Eh. What can you do. It's already published. :-p
(Strangely, it has nearly the SAME little comics between chapters as a different web-development related "For Dummies" book that I read/skimmed. Fresh out of jokes?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes, arrays are zero based.
abalfazl
Forum Commoner
Posts: 71
Joined: Mon Sep 05, 2005 10:05 pm

Post by abalfazl »

Hello!

When using of duck typing is better ?In which case do you suggest it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Variable naming is entirely personal choice. There's no "best."
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

abalfazl wrote:When using of duck typing is better ?In which case do you suggest it?
Yes. Duck typing is a natural way to solve some problems in PHP.
(#10850)
abalfazl
Forum Commoner
Posts: 71
Joined: Mon Sep 05, 2005 10:05 pm

Post by abalfazl »

Hello!

I want to know when using of duck typing is better than using of inheritance or interface in order to polymorphism?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

abalfazl wrote:I want to know when using of duck typing is better than using of inheritance or interface in order to polymorphism?
In PHP it does not matter. The reason that people discuss inheritance is because in other languages you can only do polymorphism by inheritance. PHP does not have that constraint. You can use a class polymorphically that either inherits method foo() or creates its own foo() method. Neither is better.
(#10850)
abalfazl
Forum Commoner
Posts: 71
Joined: Mon Sep 05, 2005 10:05 pm

Post by abalfazl »

Hello!

What is your idea about this:
Interfaces have two uses; To allow duck typing in static languages, and to carry meta information about a class. The former is irrelevant in PHP.

Agree?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

... Kind of what arborint just said....
Post Reply