Location: PHPKode > projects > SvnDashboard > 1.1.3.140/phpLib/functions/userFunction.funcs.php
<?php

//=============================================================================
//
// Description:
// 	Various functions for creating and calling user function pointers
//
// Functions:
//	callbackReference( String $func )
//	callUserFunction( )
//
//=============================================================================

function callbackReference($func)
{
	//=============================================================================
	// Function: callbackReference
	// Author: Bob Jackman
	// Date: ????
	// Description:	create a pointer to a function or object method
	//
	// Params:
	//	$func {string} Required:	Name of user-defined function/method to call
	//
	// Returns: {pointer}
	//=============================================================================

	// ====================  Determine if Passed String is a Function or Object Method
	if (preg_match("/^(.*)->(\w+)$/", $func, $matches)) //-- handler is an object method
	{
		$objects = explode('->', $matches[1]); // -- create array of objects

		// ====================  Recursively Form Reference to Subsequent Objects
		$first = array_shift($objects);

		if (isset($GLOBALS[$first]))
		{
			$obj = $GLOBALS[$first]; // -- make reference to 1st level object

			foreach ($objects as $object) // -- loop through additional objects
			{
				$obj = $obj->{$object};
			}

			// ====================  Check if Object Method Exists
			if (method_exists($obj, $matches[2])) // -- method exists
			{
				$func = array($obj, $matches[2]); // -- create object/method array for use with call_user_func();
			}
			else // -- method does not exist
			{
				throw new Exception ('Callback Reference: Object Method Does not Exist: \''.$func.'\'');
			}
		}
		else
		{
			throw new Exception ('Callback Reference: Object Does not Exist: \''.$matches[1].'\': Thus, Object Method Does not Exist');
		}
	}
	else // -- if handler is a function
	{
		// ====================  Check if Function Exists
		if (!function_exists($func)) // -- function does not exist
		{
			throw new Exception ('Callback Reference: Function Does not exist: \''.$func.'\'');
		}
	}

	return $func;
}

function callUserFunction()
{
	//=============================================================================
	// Function: callUserFunction
	// Author: Bob Jackman
	// Date: ????
	// Description:	call a user-defined function or method
	//
	// Params:
	//	$functionName {string} Required:	Name of user-defined function/method to
	//								call
	//	$functionArgs {mixed} Optional:	Additional arguments to pass to the
	//								user-defined function/method
	//
	// Returns: {void}
	//=============================================================================

	// ====================  Check Passed Arguments
	$args = func_get_args();
	if (!sizeof($args)) // -- no function name was passed
	{
		throw new Exception('A function or method name must be supplied');
	}
	else // -- function name was passed
	{
		$func = array_shift($args);
		if (sizeof($args) == 1) // -- there are arguments to pass to the user-defined function/method
		{
			$args = reset($args);
		}

		// ====================  Generate a Reference to the Function
		$callback = callbackReference($func);

		// ====================  Call the User-Defined Function/Method
		try
		{
			$result = call_user_func($callback, $args);
		}
		catch (Exception $e)
		{
			throw new Exception('Failed to Call User Function: '.$e->getMessage());
		}

		// ====================  Forward the User Function Return Value
		return $result;
	}
}

?>
Return current item: SvnDashboard