dev-master
9999999-dev https://github.com/ricardofbarros/inheritanceEasy Multiple Inheritance for Classes in PHP
MIT
The Requires
- php >=5.3.0
by Ricardo Barros
class multiple extend inherit inheritance
Easy Multiple Inheritance for Classes in PHP
Make multiple inheritances in PHP effortlessly, without alot of fuss and some potential headaches. It's easy as pie, trust me!, (*1)
Well it's simple, when you extend 'Inheritance' to your main class (child), you will gain a new method '__inherit', this method will accept an array with objects or the name of your classes, see To construct or not to construct for further understanding, next it will store all non-static public and protected methods and properties using Reflection., (*2)
For example, when you call a method it will trigger 'Inheritance' magic method '__call', and if this method exists in any class inherited, it will invoke this method., (*3)
Inheritances uses the following magic methods '__call', '__set', '__get', (*4)
Just follow these simple steps to install Inheritance in your project:, (*5)
Get Composer, (*6)
Run this command to install Inheritance in your project dir, (*7)
composer require ricardofbarros/inheritance:dev-master
class ClassA extends \Inheritance { ... }
Download and extract the Inheritance package into your project directory and require it and register Inheritance autoloader in your applicationâs bootstrap file., (*8)
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();
NOTE: For some more usage examples, see files in
examples
dir, (*9)
You can decide if you want to construct a class or just bypass the '__constructor', it's very simple to do that just see the example bellow., (*10)
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(); } }
Easy Multiple Inheritance for Classes in PHP
MIT
class multiple extend inherit inheritance