I'm trying to use namespaces. I want to extend a class inside a different namespace. The name of the class is the same. Example:
Parent:
namespace Base; class Section extends Skeleton { protected $id; protected $title; protected $stylesheet; }
Child:
namespace Base2; use \Base\Section; class Section extends \Base\Section { }
It is an application which uses Doctrine 2 and Zend Framework. The Skeleton class used by Base/Section is just an abstract class that contains the magic methods (__get, _set, etc).
When I try to instantiate a \Base2\Section class it throws an error:
Fatal error: Cannot declare class Base2\Section because the name is already in use in /var/www/test/application/Models/Base2/Section.php on line 7
Any idea's?
Inside a namespace, no two classes can have the same name.
Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.
Well, a class name can be the same, but only when defined in different namespace.
The class_alias() function is an inbuilt function in PHP which is used to create an alias name of the class. The functionality of the aliased class is similar to the original class.
Just use fully qualified name
namespace Base2; class Section extends \Base\Section { }
Or aliasing
namespace Base2; use \Base\Section as BSection; class Section extends BSection { }
when you say
use \Base\Section
you are pulling the Section class into your current scope, causing a conflict when you want to create a new class called Section. just omit the use statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With