PHP Doku:: Aktivieren des Überladens von Eigenschaften und Methodenaufrufen für eine Klasse - function.overload.html

Verlauf / Chronik / History: (17) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDas Verhalten von PHP beeinflussenObjekteigenschaften und Methodenaufrufe überladenFunktionen zum Überladen von Objektenoverload

Ein Service von Reinhard Neidl - Webprogrammierung.

Funktionen zum Überladen von Objekten

<<Funktionen zum Überladen von Objekten

Ausgabepufferung>>

overload

(PHP 4 >= 4.3.0)

overloadAktivieren des Überladens von Eigenschaften und Methodenaufrufen für eine Klasse

Beschreibung

void overload ( string $class_name )

Die overload()-Funktion aktiviert das Überladen von Eigenschaften und Methodenaufrufen für eine Klasse, die von class_name bestimmt wird.

Parameter-Liste

class_name

Der Name der zu überladenden Klasse als String

Rückgabewerte

Es wird kein Wert zurückgegeben.

Beispiele

Sie finden ein Beispiel im Einführungsabschnitt zu dieser Extension.


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
osminosm at gmail dot com
22.01.2009 16: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.



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",...)