PHP Classes

PHP Objects Extensions: Add functions to objects using another class

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 61 All time: 10,477 This week: 97Up
Version License PHP version Categories
php-extensible-objec 1.0.0MIT/X Consortium ...7Tools, Libraries, Data types, PHP 7, 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.

Picture of Axel Pardemann
  Performance   Level  
Name: Axel Pardemann <contact>
Classes: 11 packages by
Country: Mexico Mexico
Age: 42
All time rank: 187220 in Mexico Mexico
Week rank: 206 Up4 in Mexico Mexico Up
Innovation award
Innovation award
Nominee: 3x

Documentation

<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 (30)  
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 (30)  /  .assets  
File Role Description
  Accessible without login Plain text file buy-me-a-coffee.svg Data Auxiliary data

  Files folder image Files (30)  /  .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 (30)  /  .circleci  
File Role Description
  Accessible without login Plain text file config.yml Data Auxiliary data

  Files folder image Files (30)  /  .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 (30)  /  src  
File Role Description
Files folder imageContracts (2 files)
Files folder imageExceptions (2 files)
Files folder imageTraits (1 file)

  Files folder image Files (30)  /  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 (30)  /  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 (30)  /  src  /  Traits  
File Role Description
  Plain text file HandlesExtensionMethods.php Class Class source

  Files folder image Files (30)  /  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 (30)  /  tests  /  Feature  
File Role Description
  Plain text file ExampleTest.php Class Class source

  Files folder image Files (30)  /  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 (30)  /  tests  /  Unit  
File Role Description
  Plain text file ExtensionMethodTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:61
This week:0
All time:10,477
This week:97Up