SOliver Posted August 3, 2011 Share Posted August 3, 2011 http://stackoverflow.com/questions/6905364/what-would-be-the-best-way-to-load-with-variables-multiple-files-content-into-a On monday evening, having read the linked question, I spent a bit of time thinking of how the problem could be resolved with least amount of unnecessary processing or coding which in the end I went with more or less the includePhpFile() function stored in the procedding code. Like something out of a horror film, my internet died whilst posting an answer so I decided to expand on my answer to cover the limitations imposed by the scope of the include call. Anyway the fruit of my temporary internet-less productivity was the following which I thought at the very least may start an interesting discussion on similar php. Script has three parts, The main object, an inclusion script and example script for demonstration purposes. I aplogise if any of the comments appear pretentious or similar, I'm somewhat blind to such things and don't do it intentionally. Object: /* FunctionManager Static methods: isValidIndex($index); Methods: _cconstruct(); includePhpFile(); callFunction($key, $params = null); Members private $functions[]; Description: 1. An object designed to allow javascript style dynamic function declarations. 2. Implement above for version control. */ class FunctionManager{ /* (Array)Dynamically loaded functions */ private $functions; /* (Array)Dynamically loaded members */ private $members; /* Constructor desc: Nothing really other than instantiate Arrays. */ public function __construct() { $this->functions = Array(); $this->members = Array(); } /* include file params: $url = file location. desc: Include a php script within the scope of $this which should conform to the two rules of script inclusion for an instance of FunctionManager (assuming include is to modify $this). return Boolean, true if include, false if not, */ public function includePhpFile($url) { if(file_exists($url)){ include $url; return true; } return false; } /* params: $key = $this->functions[] key for specific function. $params = [optional] A parameter which if set is passed to the function being call using $f($this,$params), else $f($this). desc: Attempts to find this->functions[$key] and calls it's method if it exists. return: If the function exists the return will be that of the called function else it will be null. */ public function callFunction($key,$params = null) { if(FunctionManager::isValidKey($key)) if(isset($this->functions[$key])){ $function = $this->functions[$key]; if($params==null) return $function($this); else return $function($this,$params); } return null; } /* params: $index = $this->members[] index. desc: Checks $this->members[] for $index and returns the member if it exists. return: Member if the index is valid, false if it is not. */ public function getMember($key) { if(FunctionManager::isValidKey($key)) if(isset($this->members[$key])) return $this->members[$key]; return false; } /* params: $key = $this->members[] index. desc: Check that $key is a valid and set member. return: Boolean, true if set, false if not. */ public function setMember($key,$value) { if(FunctionManager::isValidKey($key)){ $this->members[$key] = $value; return true; } return false; } /* params: $key = Mixed value desc: Check that $key is a valid array index type. return: Boolean, true if valid, false if not. */ public static function isValidKey($key) { if(is_string($key)||is_numeric($key)) return true; return false; } } Sample include: /* FunctionManager - include script Description: A script which is included inside an instance of the FunctionManager object. Methods and members are stored in the functions and members arrays respectively. Notes: 1. The script is included from within an object->method which is considered to be within the scope of the object. Therefore direct declartions to private/protected methods are viable. 2. Once the include is complete and the method which requested it have finished the scope the newly included methods, which aren't technically object methods, are no longer directly accessible. 3)Methods must accept a reference to the owner object. 4)References must use the built-in public methods to change members[] values or call methods stored in functions[]. 5)Any methods added to the functions array or any other variable must be proceeded by ";" since storing closures in a variable requires a set var declaration. */ /* Add member */ $this->members['scale'] = 1; /* Add function */ $this->functions['changeScale'] = function($self,$value) { $self->setMember('scale', $value); }; Example usage: /* Example usage (sse inc.php code below) */ /* file url */ $url = 'inc.php'; /* Create new Example object */ $example = new FunctionManager(); /* Attempt to include the file located at $url*/ $example->includePhpFile($url); /*Echo default scale*/ echo'<br />OLD SCALE: '. $example->getMember('scale'); /* Set scale */ $example->callFunction("changeScale",1.5); /* Echo new scale */ echo'<br />SCALE: '. $example->getMember('scale'); Quote Link to comment https://forums.phpfreaks.com/topic/243708-javascript-esque-prototyping/ Share on other sites More sharing options...
requinix Posted August 3, 2011 Share Posted August 3, 2011 In your shoes I would replace getMember with __get, setMember with __set, and callFunction with __call. I'd also collapse member variables and methods into one set, and have __call make sure the requested thing is a callable function (while __get and __set don't care). class FunctionManager { private $_members = array(); public function import($file) { $self = $this; require_once($file); } public function __call($function, $args) { $fn = $this->__get($function); if ($fn !== null && is_callable($fn)) { return call_user_func_array($fn, $args); } else { trigger_error(E_USER_ERROR, "Not a function."); } } public function __get($name) { return (isset($this->_members[$name]) ? $this->_members[$name] : null); } public function __set($name, $value) { $this->_members[$name] = $value; } } // example.inc.php $this->OS = PHP_OS; $this->getIsWindows = function() use ($self) { return "OS " . ($self->isWindows($self->OS) ? "is" : "is not") . " Windows"; }; $this->isWindows = function($os) { return strncmp($os, "WIN", 3) == 0; }; $fm = new FunctionManager(); $fm->import("example.inc.php"); echo "OS=", $fm->OS, "\n"; echo $fm->getIsWindows(); Quote Link to comment https://forums.phpfreaks.com/topic/243708-javascript-esque-prototyping/#findComment-1251380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.