downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Fonctions sur la surcharge d'objets> <Exemples
[edit] Last updated: Fri, 25 May 2012

view this page in

Utilisation simple

Voici un exemple simple de fonctions utilisant overload() :

Exemple #1 Overload avec une classe PHP

<?php

class OO {
   var 
$a 111;
   var 
$elem = array('b' => 9'c' => 42);

   
// Fonction de callback pour la lecture de membre
   
function __get($prop_name, &$prop_value
   {
       if (isset(
$this->elem[$prop_name])) {
           
$prop_value $this->elem[$prop_name];
           return 
true;
       } else {
           return 
false;
       }
   }

   
// Fonction de callback pour l'écriture de membre
   
function __set($prop_name$prop_value
   {
       
$this->elem[$prop_name] = $prop_value;
       return 
true;
   }
}

// Ici, l'initiation de l'overload
overload('OO');

$o = new OO;
echo 
"\$o->a: $o->a\n"// print: $o->a: 111
echo "\$o->b: $o->b\n"// print: $o->b: 9
echo "\$o->c: $o->c\n"// print: $o->c: 42
echo "\$o->d: $o->d\n"// print: $o->d:

// ajout d'une nouvelle valeur au membre $elem, en programmation OOP
$o->56

// instantiation de la classe stdclass (elle existe par défaut en PHP 4)
// $val n'est pas surchargée !
$val = new stdclass;
$val->prop 555;

// Forcez "a" à être un tableau avec l'élément $val
// Mais _set() forcera cet élément dans le tableau $elem
$o->= array($val);
var_dump($o->a[0]->prop);

?>



Fonctions sur la surcharge d'objets> <Exemples
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes Utilisation simple
Anonymous 26-Jun-2011 01:20
The result for

<?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");]
?>

can be achieved by

<?php
class Foo{
    public function
method($par = null){
        echo
"call Method no paramater";
    }
}
$foo = new Foo();
echo
$foo->method("param");]
?>

Php will give access to both $foo->method() and $foo->method("param")
author at dereleased dot com 21-Dec-2009 12:30
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.
scurvysquid at yahoo dot com 03-Jun-2009 07: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 11: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 21-Jan-2009 09: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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites