Extending a class with an other namespace with the same ClassName
I'm trying to use namespaces. I want to extend a class inside a different namespace. The na开发者_Python百科me 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?
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.
精彩评论