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
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'));

 

 

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.