ricardofbarros / inheritance
PHP中类轻松实现多重继承
dev-master
2014-06-12 17:52 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 15:28:58 UTC
README
在PHP中轻松实现多重继承,无需太多麻烦和一些潜在的头痛。 它就像做饼一样简单,相信我!
它是如何工作的?
很简单,当你将 'Inheritance' 扩展到你的主类(子类)时,你将获得一个新的方法 '__inherit',这个方法将接受一个包含对象或类名的数组,详见《构建或非构建》以了解进一步的信息,然后它将使用反射存储所有非静态的公共和受保护方法和属性。
例如,当你调用一个方法时,它将触发 'Inheritance' 的魔法方法 '__call',如果这个方法存在于任何继承的类中,它将调用这个方法。
Inheritances 使用以下魔法方法 '__call', '__set', '__get'
安装
Composer 安装
只需按照以下简单步骤在项目中安装 Inheritance
-
获取 Composer
-
运行此命令以在项目目录中安装 Inheritance
composer require ricardofbarros/inheritance:dev-master
- 立即开始继承
class ClassA extends \Inheritance { ... }
手动安装
下载并解压 Inheritance 包到您的项目目录中,并在您的应用程序的引导文件中注册 Inheritance 自动加载器。
require "path/to/inheritance/src/Inheritance.php" Inheritance::registerAutoloader();
基本用法
## ClassA.php class ClassA extends \Inheritance { public function __construct() { parent::__inherit(array( new ClassB(), new ClassC() )); } public function test() { return parent::hello().' '.parent::world(); } } ## ClassB.php class ClassB { protected function hello() { return 'Hello'; } } ## ClassC.php class ClassC { protected function world() { return 'World!'; } } ## somefile.php $class = new ClassA(); // Output : Hello World! echo $class->test();
注意:有关更多用法示例,请参阅
examples
目录中的文件
构建或非构建?
您可以决定是否要构建一个类或只是绕过 '__constructor',这非常简单,请看下面的示例。
class ClassA extends \Inheritance { public function __construct() { parent::__inherit(array( 'ClassB', // This will instance the class bypassing a potential existence of a constructor new ClassC() // This will call __construct() as expected )); } public function test() { return parent::hello().' '.parent::world(); } }
功能
- 访问受保护和公共方法
- 访问受保护和公共属性
- 在无效的作用域中使用受保护的属性和方法抛出异常
- 决定您想要构建或实例化的继承类