I am trying to cascade extend a class that is registered via class_alias. I am getting a “Cannot redeclare class” warning and am wondering if what I want to do is even possible in PHP.
My aim is to be able to create a “chain” of inheritance. One\one is the base class and Two\one extends this but also replaces One\one as the class that is aliased.
That way any number of classes can extend the main one in a particular namespace without having any knowledge of the other classes in the chain/which other classes have already extended.
one.php
namespace One;
class_alias('One\one', 'one');
class one {}
two.php
namesapce Two;
class_alias('Two\one', 'one');
class one extends \one{}
index.php
<?php
require('one.php');
require('two.php');
$instance = new \one();
var_dump($instance);
You cannot have two classes with the same name in the same namespace. You are trying to assign the name
onetwice in the global namespace via theclass_aliascalls.You are using namespaces and then try to bypass them. What’s the need to do that?