/**
 *	This function is used for method aggregation will work in PHP4 and PHP5
 *
 *	@param object object
 *	@param string class_name 
 */
function my_aggregate_methods(&$object, $class_name)
{
    if (function_exists('aggregate_methods')) {
        return aggregate_methods($object, $class_name);
    }
    if (function_exists('runkit_class_adopt')) {
        return @runkit_class_adopt(get_class($object), $class_name);
    }
    if (function_exists('classkit_aggregate_methods')) {
        return @classkit_aggregate_methods(get_class($object), $class_name);
    }
    die("Function aggregate_methods() doesn't exists. This is probably because " . "PHP 5 or later is running on this server. Try install Runkit or " . "Classkit extension from PECL repository (http://pecl.php.net). " . "Useing classkit is safe with " . "PHP 5.0, but does not work with later versions of PHP. Useing runkit " . "is experimental. Type 'pecl install -f runkit' on your command line " . "for install the extension. And do not forget enable the extension in " . "your php.ini file.");
}
Example #2
0
<?php

class Foo
{
    public function bar($n)
    {
        return $n + 1;
    }
}
class Hooking
{
    public function __call($name, $args)
    {
        echo "Before {$name}!\n";
        $r = call_user_func_array([$this, "_hook_{$name}"], $args);
        echo "After {$name}!\n";
        return $r;
    }
    public static function hook($name)
    {
        runkit_method_rename(get_called_class(), $name, "_hook_{$name}");
    }
}
runkit_class_adopt('Foo', 'Hooking');
Foo::hook('bar');
$foo = new Foo();
echo $foo->bar(10);
echo "\n";
Example #3
0
 public function generate()
 {
     $classes = func_get_args();
     foreach ($classes as $className) {
         $rc = new ReflectionClass($className);
         $methods = $rc->getMethods();
         $staticMethods = array();
         foreach ($methods as $method) {
             if ($method->isStatic()) {
                 $parameters = $method->getParameters();
                 $params = array();
                 $paramNames = array();
                 foreach ($parameters as $param) {
                     $paramName = "\${$param->getName()}";
                     $paramNames[] = $paramName;
                     $code = "{$paramName}";
                     if ($param->isDefaultValueAvailable()) {
                         $defaultValue = $param->getDefaultValue();
                         if (is_array($defaultValue)) {
                             $defaultValue = var_export($defaultValue, true);
                         }
                         $code .= " = {$defaultValue}";
                     }
                     $params[] = $code;
                 }
                 $code = sprintf("public static function %s(%s) {\nif(self::hasStub('%s')) {\n\$return = self::callStub('%s', array(%s)); if(\$return instanceof StubReturns) { return \$return->getValue(); } }\nreturn %s::__%s(%s); }", $method->getName(), join(', ', $params), $method->getName(), $method->getName(), join(', ', $paramNames), $className, $method->getName(), join(', ', $paramNames));
                 //echo $code;
                 //exit;
                 $staticMethods[] = $code;
             }
             runkit_method_rename($className, $method->getName(), sprintf('__%s', $method->getName()));
         }
         $code = sprintf('class Mock%s extends Mock {
             %s
         }', $className, join(PHP_EOL, $staticMethods));
         eval($code);
         runkit_class_adopt($className, "Mock{$className}");
     }
 }