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

search for in the

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

view this page in

overload

(PHP 4 >= 4.3.0)

overloadActive la couche de contrôle des membres et méthodes

Description

void overload ( string $class_name )

overload() active le contrôle des accesseurs et appels de méthodes pour la classe class_name.

Liste de paramètres

class_name

Le nom de la classe surchargée, sous la forme d'une chaîne de caractères

Valeurs de retour

Aucune valeur n'est retournée.

Exemples

Voir un exemple dans la section d'introduction de cette partie.



add a note add a note User Contributed Notes overload
admin at skewedconcepts dot ca 06-Apr-2012 10:07
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

?>
osminosm at gmail dot com 22-Jan-2009 07:22
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.

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