2017 © Pedro PelĂĄez
 

library inheritance

Easy Multiple Inheritance for Classes in PHP

image

ricardofbarros/inheritance

Easy Multiple Inheritance for Classes in PHP

  • Thursday, June 12, 2014
  • by ricardofbarros
  • Repository
  • 1 Watchers
  • 1 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Inheritance

Make multiple inheritances in PHP effortlessly, without alot of fuss and some potential headaches. It's easy as pie, trust me!, (*1)

How does this work?

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)

Installation

Composer Install

Just follow these simple steps to install Inheritance in your project:, (*5)

  1. Get Composer, (*6)

  2. Run this command to install Inheritance in your project dir, (*7)

composer require ricardofbarros/inheritance:dev-master
  1. Start inheriting stuff right away
class ClassA extends \Inheritance {

...

}

Manual Install

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();

Basic Usage

## 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)

To construct or not to construct?

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();
    }    
}

Features

  • Access to protected and public methods
  • Access to protected and public properties
  • Throw exceptions for the use of protected properties and methods in an invalid scope
  • Decide which classes inherited you want to construct or instantiate without constructing

The Versions

12/06 2014

dev-master

9999999-dev https://github.com/ricardofbarros/inheritance

Easy Multiple Inheritance for Classes in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Ricardo Barros

class multiple extend inherit inheritance