overload

(PHP 4 >= 4.3.0)

overloadEnable property and method call overloading for a class

Description

void overload ( string $class_name )

The overload() function will enable property and method call overloading for a class identified by class_name.

Parameters

class_name

The overloaded class name, as a string

Return Values

No value is returned.

Examples

See an example in the introductory section of this part.

add a note add a note

User Contributed Notes 2 notes

up
0
admin at skewedconcepts dot ca
2 years ago
PHP does not have method overloading as with other languages. The only option is to use optional arguments.

This is a better testing scenario as described in the other post:

<?php

class Test
  
{
       function
Test($name = "stranger")
       {
           echo
"Hello, $name";
       }
   }

$t1=new Test();                            // Output : Hello, stranger
$t2=new Test('Osman Kalache');     // Output : Hello, Osman Kalache

?>
up
0
osminosm at gmail dot com
5 years ago
creating two methods with the same name won't work for sure (maybe in next versions i hope)
but for now all i could come up with something that looks like overloaded functions from the outside but still makes it a bit difficult for the one who's coding the actual class
as we can see in the code bellow i've used a default value for the $name argument, so when the Test() method is called with no arguments the $name argument is by default passed as NULL (or any value you wanna pass)
<?php

class Test
 
{
  function
Test($name=NULL)
    {
    echo
'Hello, ';
    if(
$name)
      {
      echo
$name.'<br>';
      }
    else
      {
      echo
'stranger<br>';
      }
    }
  }
 
$t1=new Test();                            // Output : Hello, stranger
$t2=new Test('Osman Kalache');     // Output : Hello, Osman Kalache

?>

the bad side of this trick is that you have to test your arguments (imagine how many IFs and ELSEs you get if you had just 5 arguments)
but still makes your classes easy to use.
To Top