dbo Posted October 5, 2007 Share Posted October 5, 2007 I've been working on a proof of concept a frameworkish thing I've been working on. I thought that variables in the "parent" file were available in included files. form.source.php <?php include_once "../Framework/xml_rule_validator.class.php"; $xrv = new XMLRuleValidator(); $xrv->loadFromFile("rules.xml"); if( count($_GET) > 0 ) { if( isset($_GET['login']) ) { $xrv->validatePhp("login"); if( $xrv->isValid() ) { //PROCESS FORM } } if( isset($_GET['test']) ) { echo "AHHHHHHHHHHHHH"; } } include_once "form.design.php"; ?> form.design.php <html> <head> <title>Form</title> </head> <body> <table> <form action="form.source.php" method="get"> <tr> <td>Username</td> <td><input type="text" name="username" id="username" value="<?php echo $xrv->getClean('username'); ?>" /></td> <td><?php echo $xrv->getError('username'); ?></td> </tr> <tr> <td>Password</td> <td><input type="text" name="password" id="password" value="<?php echo $xrv->getClean('password'); ?>" /></td> <td><?php echo $xrv->getError('password'); ?></td> </tr> <tr> <td rowspan="2" align="center"> <input type="submit" name="Login" value="login" /> <input type="submit" name="Test" value="test" /> </td> </tr> </form> </table> </body> </html> The $xrv variable is not available in the included file and results in this error: Fatal error: Call to a member function getClean() on a non-object I can think of several ways to get around this, but I'd rather not "hack". Any ideas on why I'm getting this and what a clean solution is? Quote Link to comment https://forums.phpfreaks.com/topic/72016-solved-variable-scope-and-include-files/ Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 you should have $xrv in the included file. have you echoed $xrv->getClean('username') before the include to make sure it's set before the include? Quote Link to comment https://forums.phpfreaks.com/topic/72016-solved-variable-scope-and-include-files/#findComment-362844 Share on other sites More sharing options...
dbo Posted October 5, 2007 Author Share Posted October 5, 2007 getClean just returns an empty string if the variable is not set. The problem is that it does not recognize $xrv in the included file. Quote Link to comment https://forums.phpfreaks.com/topic/72016-solved-variable-scope-and-include-files/#findComment-362853 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 my question is: Are we sure $xrv is a valid object just before the include? without having tested it myself, i would try to use the same function calls before or without the include: echo $xrv->getClean('username'); $xrv->getError('username'); if those work just before the include() or in place of the include(), i would be convinced that $xrv is a valid object. Quote Link to comment https://forums.phpfreaks.com/topic/72016-solved-variable-scope-and-include-files/#findComment-362856 Share on other sites More sharing options...
dbo Posted October 5, 2007 Author Share Posted October 5, 2007 Yes they work before the include. I was an idiot, it is in scope and working correctly. I changed the value to read 'login' instead of the name so my if statement was never true... and then I was getting the error when opening the form.design.php file (in eclipse) because when I open up just that file it isn't in scope. Yeah I'm sure that makes no sense to anyone... but it does to me and I figured it out. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/72016-solved-variable-scope-and-include-files/#findComment-362859 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 yow, i was stumped. glad you got it sorted. Quote Link to comment https://forums.phpfreaks.com/topic/72016-solved-variable-scope-and-include-files/#findComment-362862 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.