Is there anyway to for an included file to be used in a parent scope to the one it was called in? The following example is simplified, but does the same job.
In essence, a file will be included by a function, but would like the scope of the included file to be the scope of where the function that included it was called from.
main.php:
<?php
if(!function_exists('myPlugin'))
{
function myPlugin($file)
{
if(file_exists($file)
{
require $file;
return true;
}
return false;
}
}
$myVar = 'something bar foo';
$success = myPlugin('included.php');
if($success)
{
echo $myResult;
}
included.php:
<?php
$myResult = strlen($myVar);
Thanks in advance,
Alexander.
EDIT: Solution
Well, sort of, thanks to the contributions by Chacha102.
Can be called from within a class now too!
main.php
<?php
class front_controller extends controller
{
public function index_page()
{
$myVar = 'hello!';
// This is the bit that makes it work.
// I know, wrapping it in an extract() is ugly,
// and the amount of parameters that you can't change...
extract(load_file('included.php', get_defined_vars(), $this));
var_dump($myResult);
}
public function get_something()
{
return 'foo bar';
}
}
function load_file($_file, $vars = array(), &$c = null)
{
if(!file_exists($_file))
{
return false;
}
if(is_array($vars))
{
unset($vars['c'], $vars['_file']);
extract($vars);
}
require $_file;
return get_defined_vars();
}
included.php:
<?php
$myResult = array(
$myVar,
$c->get_something()
);
If you want to reference a method it has to be public, but the result is as expected:
array(2) {
[0]=>
string(6) "hello!"
[1]=>
string(7) "foo bar"
}
Now, this doesn't really have any practical use, and the only reason I wanted to know how to do this is because I am stubborn. The thought came into my head, and would not let it defeat me :D
<rant>
Thanks to all who contributed. Except the person who boo'd me. It was a simple enough question, and have now figured out that (complicated) solution exists.
Screw whether it "conforms to the PHP way of doing things". Ever told a client "Oh no, we shouldn't do that, it's not the correct way to do things!"? Thought not.
</rant>
Thanks again to Chacha102 :)