For functions, I would do something like this:
We create a folder where all functions will be stored, if functions need to be broken down by context - we do it with folders, let's say for each function a separate file is created with its name, all we need to write is a class that will receive the path to the function and load, those. the result is something unpretentious of the form:
/* singletone */ class Functions { const ROOT = '/functions/'; private static $_instance = null; private function __construct(){} private function __clone(){} public static function getInstance() { if( self::$_instance === null ) { self::$_instance = new self(); } return self::$_instance; } public function load( $path ) { $fullPath = $_SERVER['DOCUMENT_ROOT'] . self::ROOT . $path . '.php'; return file_exists( $fullPath ) ? include_once( $fullPath ) : false; } }
And in addition, we use something like this
Functions.getInstance()->load( 'view/form' ); // загрузим функцию form из группы view
Well, in general, something like that, besides, this class could then be expanded, for example, add a method for loading any "group" as a whole, etc.
If we are talking about a kind of front controller for manipulating the form Functions::call('functionName', $param1, $param2)
, then you need to look towards the PHP Closure functions. Those. do something like this (loading functions from files in this example is missing):
class Functions { public static $methods = array(); // массива функций (Closure objects) public static function register( $functionName, $function ) { self::$methods[ $functionName ] = $function; } public static function call( $functionName ) { if( self::$methods[ $functionName ] ) { $func = self::$methods[ $functionName ]; $args = array_splice(func_get_args(), 1); return call_user_func_array( $func, $args ); } } } // пользоваться так, например Functions::register('hello', function($username){ echo 'Hello ' . $username; }); Functions::register('buy', function($username){ echo 'Buy ' . $username; }) Functions::call('hello', 'Vasya'); // -> hello Vasya Functions::call('buy', 'Vasya'); // -> buy Vasya
Those. when writing any function, simply write it as:
Functions::register('functionName', function() { // function code here... });
Thus, when inclusive, the function is registered in the static associative array of the class Functions and become available for calling the form
Functions::call('functionName', param1, param2, param3, param4, ...);
All you have to do is write the combination of functionality you need from the examples I gave to the class that would best suit you directly.
UPD
Regarding the method of proxying, you can simply write the __call function for the class and add a proxy small method for the Functions class:
// метод класса Functions для облегчения проксирования public static function proxy( $name, $args ) { if( self::$methods[ $functionName ] ) { $func = self::$methods[ $functionName ]; return call_user_func_array( $func, $args ); } } // ну и пример класса class someClassName() { public function __call( $functionName, $args ) { Functions::call( $functionName, $args ); } }
Now let's say we create an instance of this class. Suppose we have a registered function hello, while the class someClassName naturally does not have it, then:
$obj = new someClassName(); $obj->hello( 1, 2, 3 ); // будет вызвана функция Functions::call('hello') // с параметрами 1, 2, 3