(Changing thread only to comp.lang.php)
"Berislav Lopac" <berislav.lopac@lopsica.com> wrote in message
news:ct822n$trf$1@garrison.globalnet.hr...[color=blue]
> Terje Slettebų wrote:[color=green]
> > "Chung Leong" <chernyshevsky@hotmail.com> wrote in message
> > news:LK6dnSGtbIGYFmncRVn-tw@comcast.com...[color=darkred]
> >> Given that PHP isn't strongly type, I don't really see the need.[/color]
> >
> > What does that got to do with it? Overloading allows you to do things
> > like:
> >
> > function print(array a) { ... }
> > function print(MyObject o) { ... }
> > function print(int i) { ... }[/color]
>
> This is not overloading -- this is *function* overloading.[/color]
Function overloading is still one kind of overloading, so your correction is
incorrect. :)
[color=blue]
> Overload, in its most general sense, is when a unit, depending on the
> circumstances, does different things.[/color]
That's a very broad interpretation, which I haven't heard anywhere else.
What you describe is usually called polymorphism (and overloading enables
one kind of polymorphisms). In general, the way I have understood
overloading, is when you may have several names in the same scope (typically
functions) - the names "overload" or coexist with each other.
[color=blue]
> Therefore, the use of the word
> overloading for the feature that enables various things to be performed[/color]
when[color=blue]
> a value is accessed is perfectly valid.[/color]
Do you have a link to something supporting that definition of overloading
(outside the PHP community)? Wikipedia defines "overloading" as
(
http://en.wikipedia.org/wiki/Overloading#Overloading):
"Overloading allows multiple *functions* taking different types to be
defined with the same name; the compiler or interpreter automatically calls
the right one." (my emphasis)
There has also been a suggestion for "class template overloading" in C++,
which would mean being able to overload templates (since the parameter lists
may distinguish them from each other, as with functions). For those familiar
with the syntax, this could look like this:
template<class T>
class some_template;
template<int i> // Different parameter kind
class some_template;
template<class T1, class T2> // Different number of parameters
class some_template;
etc.
[color=blue]
> As for your original question, i.e. why is there no "classic" overloading[/color]
in[color=blue]
> PHP, the answer is at least twofold.
>
> For one, it might break backward compatibility, which is very important
> considering the installed base of PHP4 applications.[/color]
Indeed.
[color=blue]
> Since in PHP4 you can't
> have two functions with same name in the same scope (e.g. a class, or
> globally), the overloading approach you suggest would break this.[/color]
How can that be? As you say yourself, this is currently illegal, so no valid
program may have it today. How can making something previously illegal,
legal, break existing programs? This is a classic case of "pure extension"
(not breaking backwards compatibility). The same goes for type-hints. Am I
missing something?
[color=blue]
> For two, as Chung pointed out above, there is no need. In strongly-typed
> languages, such as Java and C++, the types of method parameters are very
> important, and you have to set them at compile time; in PHP the type is[/color]
not[color=blue]
> important, and it is resolved at runtime.[/color]
And this changes what? As I've said before, at any one time, a variable has
a well-defined type. Thus, it may be used in overload resolution in a
function call. Yes, there may be conversions (like in C++ and Java), so one
may need to implement a ranking of the functions to determine "best match".
[color=blue]
> I conclude from your other posts (about visibility) that you think too
> closely within the constraints you are familiar with. Instead, you should
> get familiar with the feature of the other language and use them properly.[/color]
That about visibility and access was a minor thing. As I said in another
post, there may be good reasons for either way (and indeed, both ways were
considered during the development of C++).
[color=blue]
> Think about this: even if "classic" overloading would be possible in PHP,[/color]
is[color=blue]
> it inherently better than other options? In PHP, the example in your post
> above would look like this:
>
> function print($var) {
> if(is_array($var)) {
> // do something...
> }
> else if(is_a('MyObject', $var)) {
> // do something else...
> }
> else {
> // do something completely different...
> }
> }
>
> I can understand that it's cleaner if you do all this things in different
> functions, but this is easily remedied like this:
>
> function print($var) {
> if(is_array($var)) {
> printArray($var); // another function
> }
> else if(is_a('MyObject', $var)) {
> $var->print() // this is proper OO :)
> }
> else {
> // printing code for everything else
> }
> }[/color]
If "MyObject" is an interface (rather unlikely, given the name), then this
is the same example as the one to Chung, and, yes, it's a reasonable design.
However, it requires that each class you want to print implements that
interface (in PHP5 we have a standard interface for this, though,
toString(), which is used automatically in certain places (such as "echo
$object")). If you want to print a class that is in a third-party library
not implementing that interface, and where you can't (or shouldn't) change
it, you can't use this approach. However, from this discussion, and from
these examples, I've come to think of the following slightly esoteric way
that may be used for any type:
function printer($var) // Generic version/dispatcher
{
$function_name="print_".(is_object($var) ? get_class($var) :
gettype($var));
call_user_func($function_name,$var);
}
"printer", rather than "print" is used, as the latter is taken. A more
robust version would enable any number of parameters, but that should be
relatively easy.
Use:
class SomeClass // Some class we can't change
{
public function get_state()
{
return "1234";
}
}
// "Overloads" for printer()
function print_integer($var)
{
echo $var;
}
function print_SomeClass($var)
{
echo $var->get_state();
}
$a=1;
printer($a); // Prints "1"
$a=new SomeClass();
printer($a); // Prints "1234"
This is the closest I've found to function overloading in PHP, and I think
it's quite reasonable, given that the dispatcher class printer() never have
to be changed, and you classes don't need to implement a particular
interface. We can just add a new function, and it will work like with the
rest of the types.
If, in your example, "MyObject" is an object, rather than an interface,
then, no, then it's not "proper OO". Then it's switch-on-type, which kind of
simulates how OO works, rather than actually using inheritance-based
polymorphism (also known as OO), such as using an interface.
Regards,
Terje