Location: PHPKode > scripts > PHP Callback > php-callback/class.phpCallback.php
<?php
/*******************************************************************************
*
*   class phpCallback  version 1.0
*
*   This class is intended for management of 'callback' elements,
*   such as Objects and it's methods, or arrays with it's values, or some functions,
*   or whatever you want.
*
*   It based on a principle of the mechanism of PHP 'call_user_func_array()' function,
*   with preliminary addition of the 'callbacks' elements, and their subsequent call.
*   For a calling a callback-elements JAVA-syntax (object.method) is used as I find its more readable,
*   in comparison with PHP syntax ($object->method())
*   Anyway, I find this class very convenient and useful for myself,
*   and, if it will be useful for someone else - it will be pleasant for me. :)
*
*   Please, see the example.php for details.
*
*   On any questions, please, contact me by the following email.
*
********************************************************************************
*   Copyright (C) 2005  Konstantin S. Budylov <hide@address.com>
*   Nightwork.Studio (C) 2005 http://nightworks.ru
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation; either version 2 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program; if not, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
********************************************************************************/
class phpCallback
{
    var $__Callbacks;   //Callbacks array

    /*
        AssignCallback( $callback_name, $callback )

        Assign the callback into the array

        @access public

        @param  string $callback_name  - name for callback
        @param  mixed  $callback       - callback

        @return bool  TRUE on success or FALSE - otherwise
    */
    function AssignCallback($callback_name="", $callback)
    {
        if(is_callable($callback)){
            set_array_key($callback_name, $callback, &$this->__Callbacks);
            return true;
        } elseif (is_array($callback) && is_object($obj=&$callback[0])) {
            if(array_key_exists($callback[1],get_object_vars($obj))){
                set_array_key($callback_name, $callback, &$this->__Callbacks);
                return true;
            }
            $this->Error("Invalid callback for the callback '<i>".$callback_name."</i>'. The property '<i>".$callback[1]."</i>' is not exists in object '<i>".$callback[0]."</i>'.");
            return false;
        } elseif (is_string($callback)) {
            set_array_key($callback_name, $callback, &$this->__Callbacks);
            return true;
        }
        $this->Error("Invalid callback type for callback '<i>".$callback_name."</i>'.");
        return false;
   }

    /*
        Execute( $callback_name, $callback_args, $default )

        Executing the callback given by name, and returns its result;

        @access public

        @param  string  $callback_name   - the name of 'callback' which one was assigned by AssignCallback() method
        @param  array   $callback_args   - the array of arguments, which will be transferred to a callback (if its a function or a method, no matter otherwise)
        @param  mixed   $default         - this will be returned if errors will be occured.

        @return mixed
    */
    function Execute($callback_name="",$callback_args=array(),$default=null)
    {
        if($callback=get_array_key($callback_name, $this->__Callbacks, false)){

            if(is_callable($callback)){
                return call_user_func_array($callback,$callback_args);
            }
            if(is_array($callback)){
                $obj=&$callback[0];
                return $obj->$callback[1];
            }
            return $callback;
        }
        $this->Error("Invalid callback name for executing. The callback '<i>".$callback_name."</i>' is not exists.");
        return $default;

   }

    /*
        Simple error reporting
    */
    function Error($message="")
    {
        echo $message."<br />";
    }
}
/*

*/
?>
Return current item: PHP Callback