Jump to content

Create a wrapper method for require


optikalefx

Recommended Posts

My php needs a wrapper function for require.  Because there is a specific naming scheme thats always applied, and i want to log every require.

 

so i made $this->_require($file)

The problem is, the scope isn't carried over, so any variables that exists in the method that called _require don't exist in the required file. But they DO exist when i don't use the wrapper function.

 

So it seems in order for the variable scope to carry into the required file the require itself has to be in the same function call.

 

Is there any solution to me being able to make a wrapper method for require?

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/212079-create-a-wrapper-method-for-require/
Share on other sites

This really isn't a good idea.  Variables included/required in a function are going to be local to that function.  I can show two options but recommend that you not use a wrapper.

 

You can modify the required files to define vars something like this (then they are available globally):

$GLOABLS['myvar'] = 'something';

 

Or you can modify the function and the _require call:

function _require($file) {
   require($file);
   return get_defined_vars();
}

extract($this->_require('/path/to/some/file.php'));

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.