Jump to content

[SOLVED] Variable Scope and Include Files


dbo

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.