itrocks/extend

Some annotations to simulate more multi-inheritance in PHP

dev-master 2023-04-24 05:13 UTC

This package is auto-updated.

Last update: 2024-04-24 07:27:47 UTC


README

Hopelessly, PHP does not allow simple multiple inheritance.

Furthermore, there is no mechanism for declaring that a trait is designed to enhance a given class (or some given classes).

This little library adds a repetitive attribute named ITRocks\Extend, which allows to declare additional extends:

  • to a class: you will be able to simulate some multi-inheritance mechanisms based on them,
  • to an interface or trait: to reserve the implement/use of this to a given class,
  • to a trait: tells that the trait is designed to be used in a class that inherits a class or a class using a given trait.

Another attribute, ITRocks\Extend\Implement, allows to declare that a trait is designed to implement the methods prototyped into given interfaces.

Examples

it.rocks widely use the ITRocks\Extend attribute to declare traits that are designed to extend base classes at will, depending on your use case deployment configuration.

use ITRocks\Extend;

class My_Expandable
{
	public string $a_property;
}

interface My_Interface
{
	public function getSomething() : string;
}

#[Extend(My_Expandable::class), Implement(My_Interface::class)]
trait My_Extension
{
	public string $additional_optional_property;
	public function getSomething() : string { return $this->additional_optional_property; }
}

Application example

You may use the additional itrocks/build library to apply these inheritance mechanisms.

With the previous example code and the following configuration :

$configuration = [My_Expandable::class => [My_Extension::class]];

itrocks/build will replace all instantiations of My_Expandable into your source code by instantiations of the following built class:

class My_Expandable\B extends My_Expandable implements My_Interface
{
  use My_Extension;
}

Refer to the documentation of itrocks/build to see how.