PHP Doku:: Eine PHP-Klasse überladen - overload.examples.basic.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDas Verhalten von PHP beeinflussenObjekteigenschaften und Methodenaufrufe überladenBeispieleEine PHP-Klasse überladen

Ein Service von Reinhard Neidl - Webprogrammierung.

Beispiele

<<Beispiele

Funktionen zum Überladen von Objekten>>

Einige einfache Beispiele für die Verwendung der overload()-Funktion:

Beispiel #1 Eine PHP-Klasse überladen

<?php

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

   
// Callback-Methode für die Abfrage einer Eigenschaft
   
function __get($prop_name, &$prop_value)
   {
       if (isset(
$this->elem[$prop_name])) {
           
$prop_value $this->elem[$prop_name];
           return 
true;
       } else {
           return 
false;
       }
   }

   
// Callback-Methode für das Setzen einer Eigenschaft
   
function __set($prop_name$prop_value)
   {
       
$this->elem[$prop_name] = $prop_value;
       return 
true;
   }
}

// Hier überladen wir das OO-Objekt
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:

// füge einen neuen Eintrag zum $elem-Array in OO hinzu
$o->56;

// instanziiere stdclass (ist in PHP 4 eingebaut)
// $val ist nicht überladen!
$val = new stdclass;
$val->prop 555;

// setze "a" als Array mit dem $val-Objekt als Value
// __set() wird dies aber trotzdem dem $elem-Array zuordnen
$o->= array($val);
var_dump($o->a[0]->prop);

?>


4 BenutzerBeiträge:
- Beiträge aktualisieren...
author at dereleased dot com
21.12.2009 21: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
4.06.2009 4: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.03.2009 20: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.01.2009 6: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)