groundwar Posted July 4, 2007 Share Posted July 4, 2007 I'm looking for help on loading the following constant dynamically from a $_GET variable. As in ?module=MODULE_ACCOUNT_SMALL would load the following class call. define("MODULE_ACCOUNT_SMALL", "\$account = new account(); \$account->account_small(array(ACCOUNT_id => \$_SESSION[ACCOUNT_id]));"); Here's how this works: inline, I do a call like this: load_module(MODULE_ACCOUNT_SMALL); which in turn calls this: function load_module($module){ //Check if we're outputting the directives if(TEST_MODE_O_MODULE_DIR){ echo "<h5>Loaded Module from Directive:</h5>"; echo $module . "<br>"; } //Evaluate the code and output eval($module); } which evals the class/module call which again looks like this: $account = new account(); $account->account_small(array(ACCOUNT_id => $_SESSION[ACCOUNT_id])); this code loads this: function account_small( $criteria ){ // Get the account record based on the criteria $record = select( $criteria, __CLASS__ ); // Include SMALL account information - Output the module file name if test mode is set module(__CLASS__ . "." . __FUNCTION__,$record[0]); } which in turn spits out the module file generated on the fly: function module($module,$variable){ //Write out the file name if applicable if(TEST_MODE_O_MODULE){ echo "<h5>Loaded Module:</h5>"; echo MODULE_DIRECTORY . __FUNCTION__ . "." . $module . MODULE_EXTENSION . "<br>"; } //Output the included file include $_SERVER['DOCUMENT_ROOT'] . MODULE_DIRECTORY . __FUNCTION__ . "." . $module . MODULE_EXTENSION; } or in the case of other modules, it spits out an error file generated on the fly: function error($error){ //Write out the file name if applicable if(TEST_MODE_O_ERROR){ echo "<h5>Loaded Error:</h5>"; echo ERROR_DIRECTORY . __FUNCTION__ . "." . $error . ERROR_EXTENSION . "<br>"; } //Output the included file include ERROR_DIRECTORY . __FUNCTION__ . "." . $error . ERROR_EXTENSION; } I asked this yesterday to no avail. I want to avoid using a if/case statement if possible. Thanks for the help. J Link to comment https://forums.phpfreaks.com/topic/58421-solved-class-call-from-a-_get-variable/ Share on other sites More sharing options...
trq Posted July 4, 2007 Share Posted July 4, 2007 I really think your over complicating this with the use of constants. Why not call your class from within a function then dynamically call that function if need be? eg; <?php function createaccount() { $account = new account(); $account->account_small(array(ACCOUNT_id => $_SESSION[ACCOUNT_id])); } if (isset($_GET['action'])) { if (function_exists($_GET['action'])) { $_GET['action'](); } } ?> Even this I would do differently. You need to make all your so called modules share the same interface. This way you could simply do something like. <?php class foo { function test() { echo 'this is foo->test'; } } class bar { function test() { echo 'this is bar->test'; } } if (isset($_GET['action'])) { if (class_exists($_GET['action'])) { $obj = new $_GET['action'](); $obj->test(); } } ?> Link to comment https://forums.phpfreaks.com/topic/58421-solved-class-call-from-a-_get-variable/#findComment-289709 Share on other sites More sharing options...
groundwar Posted July 4, 2007 Author Share Posted July 4, 2007 Overcomplicated? Yes. Simply put I've worked very hard to maintain naming conventions so the all DB tables match identical class names, and modules return either an error page or module (form/output) page based on that class/module. class account { function bar(){} } On success would return: module.account.bar.php Whereas on error it would return: error.account.bar.php of r Because I'm managing a %$#load lot of tables this greatly streamlines the creation of new tables and classes and modules/errors. More importantly it greatly helps my designers because all they have to work with is a simply little PHP module: <strong><? echo $variable["name_first"];?> <? echo $variable["name_last"];?></strong> <br /> <!-- Username: <? echo $variable["username"];?><br /> Password: <? echo $variable["password"];?><br /> --> Email: <? echo $variable["email"];?><br /> Phone: <? echo $variable["phone"];?><br /> Fax: <? echo $variable["fax"];?><br /> Address 1: <? echo $variable["address_1"];?><br /> <? echo $variable["address_2"];?><br /> <? echo $variable["city"];?>, <? echo $variable["territory"];?>, <? echo $variable["country"];?><br /> Zip Code: <? echo $variable["zip"];?><br /> It saves them from looking through any code aside from the variable names I pass to each module. Then I can leave them to plug in modules where they please: <div id="MODULE_POINT_SMALL"> <? //-------------------------------------------------------------------------------// // // Output SMALL Points Information // //-------------------------------------------------------------------------------// load_module(MODULE_POINT_SMALL); ?> </div> Basically, it just saves everyone a lot of time. On a programatic level it is complicated, but for me to add a new table/class and subsequent module/error is such an enormous saving of time that the complication is worth it. Moreso, designers can't $#% up the code badly. If I were to remove the classes this just convolutes everything, you know? I guess to some degree this is a little like SMARTY. Anyhoo, the reason I need to load a CONSTANT dynamically is because I'm using AJAX to reload modules when form information is submitted. This keeps the page from ever needing a reload. All form static output is just reloaded into a div. All I need to do it pass which class->module needs to be reloaded. I pass this in the form, do a check if the CONSTANT exists, and the div is reloaded. Because I've gone as far as making my CSS classes identical, the designers know exactly what module needs to go where. Is so streamlined it's sickening. I can accomplish what I want with an IF/CASE statement, but then I have to put in constant's twice which adds work to the whole process. Bad practice. Does this clear things up a little? Suggestions? J Link to comment https://forums.phpfreaks.com/topic/58421-solved-class-call-from-a-_get-variable/#findComment-289726 Share on other sites More sharing options...
trq Posted July 4, 2007 Share Posted July 4, 2007 I'm not real clear about what your actual problem is though. If you can call this inlione and it works... load_module(MODULE_ACCOUNT_SMALL); Are you saying that calling.... load_module($_GET['module']); does not? Link to comment https://forums.phpfreaks.com/topic/58421-solved-class-call-from-a-_get-variable/#findComment-289730 Share on other sites More sharing options...
trq Posted July 4, 2007 Share Posted July 4, 2007 This may help.... load_module(constant($_GET['module'])); Link to comment https://forums.phpfreaks.com/topic/58421-solved-class-call-from-a-_get-variable/#findComment-289732 Share on other sites More sharing options...
groundwar Posted July 4, 2007 Author Share Posted July 4, 2007 This worked! load_module(constant($_GET['module'])); Alternatively, this: load_module($_GET['module']); Returned a parse error unexpected $, which was being caught in the eval statement under load module. I suspect you knew that as you updated me with the constant function which I was unaware of. So then I do a little error checking so if things go awry the developer knows what the issue is: // If if constant is defined if ( defined( $_GET['module'] ) ){ // Output the module load_module(constant($_GET['module'])); } else { echo "The module " . $_GET['module'] . " requested does not exist."; } Bang. Dummy proof! . . . . . . . Well, not really. THANKS FOR THE HELP MATE!!!!! Cheers. J Link to comment https://forums.phpfreaks.com/topic/58421-solved-class-call-from-a-_get-variable/#findComment-289742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.