PHP Classes

How Can PHP Add Function to Object Dynamically Using the Package Prototype Trait: Add functions dynamically to objects

Recommend this page to a friend!
  Info   View files Documentation   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-23 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 14 This week: 14All time: 11,302 This week: 4Up
Version License PHP version Categories
prototypetrait 1.0.0MIT/X Consortium ...5PHP 5, Language, Traits
Description 

Author

This package can add functions dynamically to objects.

It provides a trait to use by a class to allow applications to add new functions to existing objects of that class.

Applications can call the trait on a class that uses this trait to specify a callback function to associate to a class function with a given name.

Picture of Stefano Azzolini
  Performance   Level  
Name: Stefano Azzolini <contact>
Classes: 4 packages by
Country: Italy Italy
Innovation award
Innovation award
Nominee: 2x

Documentation

PrototypeTrait

Simulate Javascript's prototype based object definition in PHP (using Traits)

This trait can be used for expanding a class definition adding methods to all active instances. It's a sort of IoC/Decorator pattern, not a truly Prototype (PHP use classic OOP).

trait Prototype {
    public static $prototype=[];
    public function __call($n,$p){
        $r = static::$prototype;
        if(isset($r[$n]) && is_callable($r[$n]))
            return call_user_func_array($r[$n],$p);
        throw new Exception('Tried to call unknown method '.get_class($this).'::'.$n);
    }
}

Tweet sized version :

trait Prototype{static$prototype=[];function __call($n,$p){$v=static::$prototype;return$v[$n]?call_user_func_array($v[$n],$p):¥();}}

How to use

class TestClass {
    use Prototype;
}

$test = new TestClass;
$test->hello(); // Error! Method TestClass::hello is not defined

// Add the hello() method to TestClass prototype
TestClass::$prototype['hello'] = function(){
  echo 'Hello!';
};

$test->hello(); // Hello!

// Create a new Test instance
$test2 = new TestClass;

$test->hello(); // Hello!

License (MIT)

Copyright (c) 2013 Stefano Azzolini

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


  Files folder image Files  
File Role Description
Plain text file PrototypeTrait.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:14
This week:14
All time:11,302
This week:4Up