Something to note,
If the $original class has not yet been defined or loaded, the auto loader will be invoked in order to try and load it.
If the class for which you are trying to create an alias does not exist, or can not be loaded with the auto loader, you will generate a PHP Warning.
class_alias
(PHP 5 >= 5.3.0)
class_alias — Crée un alias de classe
Description
bool class_alias
([ string $original
[, string $alias
]] )
Créé un alias appelé alias de la classe original. L'alias est en tous points similaire à la classe originale.
Liste de paramètres
- original
-
La classe originale.
- alias
-
Le nom de l'alias de la classe.
Valeurs de retour
Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.
Exemples
Exemple #1 Exemple avec class_alias()
<?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);
?>
L'exemple ci-dessus va afficher :
bool(true) bool(false) bool(true) bool(true) bool(true) bool(true) bool(true)
Voir aussi
- get_parent_class() - Retourne le nom de la classe parente d'un objet
- is_subclass_of() - Détermine si un objet est une sous-classe
adam at adamhahn dot com
06-Sep-2011 12:13
programmer-comfreek at hotmail dot com
15-Aug-2011 09:38
If you defined the class 'original' in a namespace, you will have to specify the namespace(s), too:
<?php
namespace ns1ns2ns3;
class A {}
class_alias('ns1\ns2\ns3\A', 'B');
/* or if you want B to exist in ns1\ns2\ns3 */
class_alias('ns1\ns2\ns3\A', 'ns1\ns2\ns3\B');
?>
nicolas dot grekas+php at gmail dot com
31-Dec-2010 01: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-Dec-2010 02: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
03-Sep-2009 03: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.
