PHP Doku:: Creates an alias for a class - function.class-alias.html

Verlauf / Chronik / History: (35) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenKlassen- und ObjektinformationenKlassen- und Objekt-Funktionenclass_alias

Ein Service von Reinhard Neidl - Webprogrammierung.

Klassen- und Objekt-Funktionen

<<call_user_method

class_exists>>

class_alias

(PHP 5 >= 5.3.0)

class_aliasCreates an alias for a class

Beschreibung

boolean class_alias ([ string $original [, string $alias ]] )

Creates an alias named alias based on the defined class original. The aliased class is exactly the same as the original class.

Parameter-Liste

original

The original class.

alias

The alias name for the class.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 class_alias() example

<?php

class foo { }

class_alias('foo''bar');

$a = new foo;
$b = new bar;

// the objects are the same
var_dump($a == $b$a === $b);
var_dump($a instanceof $b);

// the classes are the same
var_dump($a instanceof foo);
var_dump($a instanceof bar);

var_dump($b instanceof foo);
var_dump($b instanceof bar);

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
nicolas dot grekas+php at gmail dot com
31.12.2010 10:09
At first, you might wonder that:
<?php class A {}; class_alias('A', 'B'); ?>

is equivalent to:
<?php class A {}; class B extends A {}; ?>

BUT when derivation creates a new class name - that means, you can then instantiate a new kind of objects - aliasing is just what it says: a synonym, so objects instantiated with the aliased name are of the exact same kind of objects instantiated with the non-aliased name.

See this code for example:
<?php
class A {};
class
B1 extends A {};
class_alias('A', 'B2');

$b1 = new B1; echo get_class($b1); // prints B1
$b2 = new B2; echo get_class($b2); // prints A !
?>
nicolas dot grekas+php at gmail dot com
30.12.2010 23:41
class_alias also works for interfaces!

<?php
interface foo {}
class_alias('foo', 'bar');
echo
interface_exists('bar') ? 'yes!' : 'no'; // prints yes!
?>
paul [dot] kotets [at] gmail [dot] com
3.09.2009 12:43
This function will appear in PHP 5.3 (at least I can use it with PHP 5.3, build Aug 7 2009 08:21:14)
For older versions of PHP I wrote the next function:

<?php
if (!function_exists('class_alias')) {
    function
class_alias($original, $alias) {
        eval(
'abstract class ' . $alias . ' extends ' . $original . ' {}');
    }
}
?>

Keyword 'abstract' is used for classes, which defines abstract methods.
This function is used in autoload purposes (when I extend classes), so abstract keyword doesn't broke anything for me.



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