Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (2) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-06-23 (2 days ago) | Not yet rated by the users | Total: 14 This week: 14 | All time: 11,302 This week: 4 |
Version | License | PHP version | Categories | |||
prototypetrait 1.0.0 | MIT/X Consortium ... | 5 | PHP 5, Language, Traits |
Description | Author | |
This package can add functions dynamically to objects. |
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):¥();}}
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!
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.
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.