PHP Classes
elePHPant
Icontem

PHP Objects Extensions: Add functions to objects using another class

Recommend this page to a friend!
  Info   View files Documentation   View files View files (30)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-05-04 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 52 All time: 9,524 This week: 300Up
Version License PHP version Categories
php-extensible-objec 1.0.0MIT/X Consortium ...7Utilities and Tools, Libraries, Data ..., P..., T...
Description Author

This package can add functions to objects using another class.

It provides an interface named Extensible that defines functions for registering, unregistering, checking and getting functions that can be used as extensions of a class.

Classes that benefit from this package can implement this interface by using a trait provided in this package that use another class to provide the implementation of each the extension function that is registered.

  Performance   Level  
Name: Axel Pardemann <contact>
Classes: 4 packages by
Country: Mexico Mexico

Details

<div align="center"> <h1>PHP Extensible Objects</h1> <p align="center">

<a href="https://packagist.org/packages/norse-blue/extensible-objects"><img alt="Stable Release" src="https://img.shields.io/packagist/v/norse-blue/extensible-objects.svg?color=%235e81ac&style=popout-square"></a>
<a href="https://circleci.com/gh/norse-blue/php-extensible-objects/tree/master"><img alt="Build Status" src="https://img.shields.io/circleci/project/github/norse-blue/php-extensible-objects/master.svg?color=%23a3be8c&style=popout-square"></a>
<a href="https://php.net/releases"><img alt="PHP Version" src="https://img.shields.io/packagist/php-v/norse-blue/extensible-objects.svg?color=%23b48ead&style=popout-square"></a>
<a href="https://codeclimate.com/github/norse-blue/php-extensible-objects/maintainability"><img src="https://api.codeclimate.com/v1/badges/253dcf3f7fd57dab4150/maintainability" /></a>
<a href="https://codeclimate.com/github/norse-blue/php-extensible-objects/test_coverage"><img src="https://api.codeclimate.com/v1/badges/253dcf3f7fd57dab4150/test_coverage" /></a>
<a href="https://packagist.org/packages/norse-blue/extensible-objects"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/norse-blue/extensible-objects.svg?color=%235e81ac&style=popout-square"></a>
<a href="https://packagist.org/packages/norse-blue/extensible-objects"><img alt="GitHub" src="https://img.shields.io/github/license/norse-blue/php-extensible-objects.svg?color=%235e81ac&style=popout-square"></a>

</p> </div> <hr>

Extensible Objects is a PHP library that provides the mechanisms to dynamically add extension methods to objects.

Installation

>Requirements: >- PHP 7.3+

Install Extensible Objects using Composer:

composer require norse-blue/extensible-objects

Usage

Create a base class that is to be extensible. To be extensible it has to implement NorseBlue\ExtensibleObjects\Contracts\Extensible and it should use NorseBlue\ExtensibleObjects\Traits\HandlesExtensionMethods:

namespace App;

use NorseBlue\ExtensibleObjects\Contracts\Extensible;
use NorseBlue\ExtensibleObjects\Traits\HandlesExtensionMethods;

class MyObject implements Extensible {
    use HandlesExtensionMethods;
}

That's all there is to it. Now you can add extension methods to the class. The best way to do so is to create an extension method class. This class must implement NorseBlue\ExtensibleObjects\Contracts\ExtensionMethod interface.

This is a special class that is invokable and returns a callback that will become the base class extension method. You can pass any number of parameters to the __invoke method and you have to pass them to the callback.

The only caveat is they all have to be optional (have a default value):

namespace App\Extensions;

class MyObjectExtension implements ExtensionMethod {
    public function __invoke() {
        return function($param1, $param2, $param3) {
            $str = '$param1: ' . $param1 . PHP_EOL . '$param2: ' .$param2 . PHP_EOL . '$param3: ';
            
            if (is_array($param3)) {
                foreach($param3 as $item) {
                    $str .= $item . ', ';
                }
            } else {
              $str .= 'Not an array';
            }
                        
            return rtrim($str, ', ');
        }
    }
}

Now you have to register the extension method anywhere in your code (obviously before using it).

To register the method you need to call the base class registerExtensionMethod function and pass it a name with what you will call the extension and the callable (in this case the invokable class name):

use App\MyObject;
use App\Extensions\MyObjectExtension;

MyObject::registerExtensionMethod('my_method', MyObjectExtension::class);

Now you can use your method as it were part of the class:

$obj = new MyObject;
echo $obj->my_method('My string', 3, ['foo', 'bar', 'baz']);

The above code will output:

$param1: My string
$param2: 3
$param3: foo, bar, baz

The extension method will be run in the class context, as it were declared inside the class. Being so, you have access to everything inside the base class (even private properties and methods). The extension method does not have to inherit from the base class, but doing so will help your IDE with auto completion and static analysis if using some class methods and properties.

For more examples look in the tests folder.

Documentation

For the full documentation refer to the docs folder.

Changelog

Please refer to the CHANGELOG.md file for more information about what has changed recently.

Contributing

Contributions to this project are accepted and encouraged. Please read the CONTRIBUTING.md file for details on contributions.

Credits

Security

If you discover any security related issues, please email security@norse.blue instead of using the issue tracker.

Support the development

Do you like this project? Support it by donating

<a href="https://www.buymeacoffee.com/axelitus"><img src=".assets/buy-me-a-coffee.svg" width="180" alt="Buy me a coffee"></img></a>

License

Extensible Objects is open-sourced software licensed under the MIT license.

  Files folder image Files  
File Role Description
Files folder image.assets (1 file)
Files folder image.changelog (2 files)
Files folder image.circleci (1 file)
Files folder image.github (4 files)
Files folder imagesrc (3 directories)
Files folder imagetests (1 file, 3 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .phpcs.xml.dist Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file extensible-objects.sublime-project Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpstan.neon.dist Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .assets  
File Role Description
  Accessible without login Plain text file buy-me-a-coffee.svg Data Auxiliary data

  Files folder image Files  /  .changelog  
File Role Description
  Accessible without login Plain text file ROADMAP.md Data Auxiliary data
  Accessible without login Plain text file UNRELEASED.md Data Auxiliary data

  Files folder image Files  /  .circleci  
File Role Description
  Accessible without login Plain text file config.yml Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
  Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
  Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data
  Accessible without login Plain text file PULL_REQUEST_TEMPLATE.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageContracts (2 files)
Files folder imageExceptions (2 files)
Files folder imageTraits (1 file)

  Files folder image Files  /  src  /  Contracts  
File Role Description
  Plain text file Extensible.php Class Class source
  Plain text file ExtensionMethod.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file ClassNotExtensionMethod.php Class Class source
  Plain text file ExtensionNotCallableException.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file HandlesExtensionMethods.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageFeature (1 file)
Files folder imageHelpers (4 files)
Files folder imageUnit (1 file)
  Plain text file TestCase.php Class Class source

  Files folder image Files  /  tests  /  Feature  
File Role Description
  Plain text file ExampleTest.php Class Class source

  Files folder image Files  /  tests  /  Helpers  
File Role Description
  Plain text file DynamicMethodUsingPrivateValue.php Class Class source
  Plain text file DynamicMethodUsingProtectedValue.php Class Class source
  Plain text file FooObject.php Class Class source
  Plain text file SimpleObject.php Class Class source

  Files folder image Files  /  tests  /  Unit  
File Role Description
  Plain text file ExtensionMethodTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:0
All time:9,524
This week:300Up