class_alias

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

class_aliasErzeugt einen Alias für eine Klasse

Beschreibung

function class_alias(string $class, string $alias, bool $autoload = true): bool

Erzeugt einen Alias namens alias basierend auf der benutzerdefinierten Klasse class. Die abgeleitete Klasse ist genau die gleiche wie die Originalklasse.

Der Klassen-Alias darf keines der reservierten Wörter von PHP sein.

Hinweis:

Seit PHP 8.3.0 kann mit class_alias() auch ein Alias einer internen Klasse erstellt werden.

Parameter-Liste

class

Die Originalklasse

alias

Der Alias-Name für die Klasse

autoload

Gibt an, ob automatisch geladen werden soll, wenn die Originalklasse nicht gefunden wird

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Changelog

Version Beschreibung
8.3.0 Mit class_alias() kann nun ein Alias einer internen Klasse erstellt werden.

Beispiele

Beispiel #1 class_alias()-Beispiel

<?php

class Foo { }

class_alias('Foo', 'Bar');

$a = new Foo;
$b = new Bar;

// die Objekte sind identisch
var_dump($a == $b, $a === $b);
var_dump($a instanceof $b);

// die Klassen sind identisch
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)

Anmerkungen

Hinweis:

In PHP wird bei Klassennamen nicht zwischen Groß- und Kleinschreibung unterschieden, was sich auch in dieser Funktion widerspiegelt. Aliase, die mit class_alias() erstellt werden, werden in Kleinbuchstaben deklariert. Das bedeutet, dass im Fall der Klasse MyClass durch den Aufruf class_alias('MyClass', 'MyClassAlias') eine neue Alias-Klasse namens myclassalias erstellt wird.

Siehe auch

  • get_parent_class() - Gibt den Namen der Elternklasse eines Objektes zurück
  • is_subclass_of() - Prüft ob ein Objekt von der angegebenen Klasse abstammt oder sie implementiert

add a note

User Contributed Notes 14 notes

up
30
mweierophinney at gmail dot com
13 years ago
class_alias() gives you the ability to do conditional imports.

Whereas the following will not work:

<?php
namespace Component;

if (version_compare(PHP_VERSION, '5.4.0', 'gte')) {
    use My\ArrayObject;
} else {
    use ArrayObject;
}

class Container extends ArrayObject
{
}
?>

the following, using class_alias, will:

<?php
namespace Component;

if (version_compare(PHP_VERSION, '5.4.0', 'lt')) {
    class_alias('My\ArrayObject', 'Component\ArrayObject');
} else {
    class_alias('ArrayObject', 'Component\ArrayObject');
}

class Container extends ArrayObject
{
}
?>

The semantics are slightly different (I'm now indicating that Container extends from an ArrayObject implementation in the same namespace), but the overall idea is the same: conditional imports.
up
11
robsonvnasc at gmail dot com
9 years ago
It also works with Traits!

<?php 
trait Foo {}
class_alias("Foo","Bar");
echo trait_exists("Bar") ? 'yes' : 'no'; 
?>
//yes
up
18
programmer-comfreek at hotmail dot com
15 years ago
If you defined the class 'original' in a namespace, you will have to specify the namespace(s), too:
<?php
namespace ns1\ns2\ns3;

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');
?>
up
17
nicolas dot grekas+php at gmail dot com
15 years ago
class_alias also works for interfaces!

<?php
interface foo {}
class_alias('foo', 'bar');
echo interface_exists('bar') ? 'yes!' : 'no'; // prints yes!
?>
up
6
stanislav dot eckert at vizson dot de
11 years ago
class_alias() creates aliases only for user defined classes, not for classes supplied by PHP (PHP will show the warning "First argument of class_alias() must be a name of user defined class"). To create aliases for every kind of classes, use namespaces:

<?php

// Does not work
class_alias("ZipArchive", "myZip");

// Creates class alias "myZip" of class "ZipArchive"
use \ZipArchive as myZip;

?>
up
1
Hayley Watson
8 years ago
The alias really is an alias for the existing class. It's not a new class of any kind - whether by inheritance or otherwise; it doesn't just look and behave exactly like the existing class; it really is the same class.

<?php

class foo
{
    public static $count = 0;
}

class_alias('foo', 'bar');

bar::$count++;

echo foo::$count; // Output: 1

echo get_class(new Bar); // Output: foo
?>
Note in the last line there that aliases are just as case-insensitive as "genuine" class names.
up
1
elliseproduction at gmail dot com
6 years ago
You can add a alias inside a class :

<?php

class foo{

    function __construct(){
        
        echo('yes!');
    
    }
}

class bar {

    function __construct(){
        
        class_alias('foo', 'fooAlias');
    
    }
    
    function test(){
        
        new fooAlias;
        
    }

}

$bar=new bar;

$bar->test(); // yes!
up
2
sergey dot karavay at gmail dot com
13 years ago
At first, you might wonder that:
<?php class A {}; class_alias('A', 'B'); ?>

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

class_alias is NOT equivalent to class extending! Private methods/properties are unseen in child classes, but in alias classes they are.
up
2