While the term "overloading" in many other languages specifically means declaring multiple methods with the same name and having the compiler intelligently select which one should be run at any given time, this (as some have pointed out) is not the case in PHP, for a very simple yet important reason:
PHP is a weakly/dynamically typed language; the only exceptions to this rule are constants (which have certain type restrictions, e.g. array) and Type-Hinted method arguments. Because of this, traditional overloading style is simply not possible, as there is no way for the interpreter to know which method to call (reliably).
However, with PHP's ability to accept an indefinite number of undefined arguments via func_get_args() and related functions, as well as simply provide defaults to values to allow you to declare just one method and perform simple tests to see if some/all of the arguments were indeed passed, traditional means of method overloading are not entirely necessary.
It may not be what you're used to, but it works.
Uso básico
Algunos ejemplos sencillos sobre el uso de la función overload():
Example #1 Sobrecargar una clase de PHP
<?php
class OO {
var $a = 111;
var $elem = array('b' => 9, 'c' => 42);
// Método de llamada de retorno para obtener una propiedad
function __get($nombre_prop, &$valor_prop)
{
if (isset($this->elem[$nombre_prop])) {
$valor_prop = $this->elem[$nombre_prop];
return true;
} else {
return false;
}
}
// Método de llamada de retorno para establecer una propiedad
function __set($nombre_prop, $valor_prop)
{
$this->elem[$nombre_prop] = $valor_prop;
return true;
}
}
// Aquí sobrecargamos el objeto OO
overload('OO');
$o = new OO;
echo "\$o->a: $o->a\n"; // imprime: $o->a: 111
echo "\$o->b: $o->b\n"; // imprime: $o->b: 9
echo "\$o->c: $o->c\n"; // imprime: $o->c: 42
echo "\$o->d: $o->d\n"; // imprime: $o->d:
// añadir un nuevo elemento a la matriz $elem de OO
$o->x = 56;
// instanciar stdclass (está incluido en PHP 4)
// ¡$val no está sobrecargada!
$val = new stdclass;
$val->prop = 555;
// Establecer "a" para que sea una matriz con el objeto $val en él
// Pero __set() pondrá esto en la matriz $elem
$o->a = array($val);
var_dump($o->a[0]->prop);
?>
author at dereleased dot com
21-Dec-2009 08:30
scurvysquid at yahoo dot com
04-Jun-2009 02:43
Overloading is defining a method multiple times with different input parameters, then conditionally running whichever method matches the provided input.
It looks like the "overload" function just enables magic methods for a class, which can be used in implementing an extremely janky simulation of overloading.
Also, it seems that magic methods are enabled by default in php5 regardless of whether the overload function has been run on a class.
Anonymous
26-Mar-2009 07:48
You can't redeclare because PHP won't know which one to use. Use a default value and test conditionally instead such as
<?php
public function method($arg=false){
if($arg==false){
//do something
} else {
//do something else
}
}
?>
ly dot sitthykun at yahoo dot com
22-Jan-2009 05:47
<?php
class Foo{
public function method(){
echo "call Method no paramater";
}
public function method($par){
echo "call Method has a paramater";
}
}
$foo = new Foo();
echo $foo->method("param");]
?>
#output:
Fatal error: Cannot redeclare Foo::method() in class_overload.php on line 6
why?
we can not create the same method name.
