Location: PHPKode > scripts > Func > FLY/Func/Method.php
<?php

/** @see FLY_Func_Interface */
require_once 'FLY/Func/Interface.php';


/**
 * This class can be used to register and call global functions.
 *
 * @package FLY
 * @subpackage Func
 * @author Yves Feupi Lepatio
 */
class FLY_Func_Method implements FLY_Func_Interface
{
    /**
     * The reflection method.
     * @var ReflectionMethod
     */
    protected $_reflectionMethod = null;

    /**
     * The class name of the reflection method.
     * @var string
     */
    protected $_className = '';

    /**
     * getReflectionMethod()
     *
     * @return ReflectionMethod
     */
    public function getReflectionMethod()
    {
        return $this->_reflectionMethod;
    }

    /**
     * Constructor
     *
     * @param string $className  The name of the class.
     * @param string $methodName The name of the public method.
     * @return void
     * @throws FLY_Func_ClassNotFoundException
     * @throws FLY_Func_MethodNotFoundException
     * @throws FLY_Func_MethodNotPublicException
     */
    public function __construct($className, $methodName)
    {
        if ( ! class_exists($className))
        {
            /** @see FLY_Func_ClassNotFoundException */
            require_once 'FLY/Func/ClassNotFoundException.php';
            throw new FLY_Func_ClassNotFoundException('The class `' . $className . '` is not found');
        }
        if ( ! method_exists($className, $methodName))
        {
            /** @see FLY_Func_MethodNotFoundException */
            require_once 'FLY/Func/MethodNotFoundException.php';
            throw new FLY_Func_MethodNotFoundException('The method `' . $methodName . '()` of the class `' . $className . '` is not found');
        }

        $reflectionMethod = new ReflectionMethod($className, $methodName);

        if ( ! $reflectionMethod->isPublic())
        {
            /** @see FLY_Func_MethodNotPublicException */
            require_once 'FLY/Func/MethodNotPublicException.php';
            throw new FLY_Func_MethodNotPublicException(
                sprintf('The private or protected method `%s()` of the class `%s` should be set to public in order to be invoked later',
                        $reflectionMethod->getName(),
                        $reflectionMethod->getDeclaringClass()->getName()));
        }

        $this->_reflectionMethod = $reflectionMethod;
        $this->_className        = $className;
    }

    /**
     * @see FLY_Func_Interface::invoke()
     */
    public function invoke()
    {
        $params     = func_get_args();
        $evalParams = '';

        for ($i = 0, $length = func_num_args(); $i < $length; $i++)
        {
            $evalParams .= '$params[' . $i . '], ';
        }
        $evalParams = substr($evalParams, 0, -2);

        $reflectionMethod = $this->_reflectionMethod;

        if ($reflectionMethod->isStatic())
        {
            return (($evalParams == '')
                    ? $reflectionMethod->invoke(null)
                    : $reflectionMethod->invoke(null, eval('return ' . $evalParams . ';')));
        }
        // Instance method
        return (($evalParams ==  '')
                ? $reflectionMethod->invoke(new $this->_className())
                : $reflectionMethod->invoke(new $this->_className(), eval('return ' . $evalParams . ';')));
    }
}
Return current item: Func