views:

131

answers:

3

Hello

I'm developing own CMS and i want to implement functionality to dynamically include existing PHP script which are located on the server.

there is variable called $page_content which contains page contents including HTML and JS code, also it contains some text recognised by regex, recognised text is then processed and replaced with desired dynamically created data. I also would like to trigger including new scripts by using that regex mechanism but here is a problem because regex recognition is solved by function and it seems that if i do "include" or "require" inside of function included script is limited by function variable scope so i can't get behaviour i need.

What should i do to make things works as i want, i mean that i can make bigger use of these dynamically included scripts.

Thanks in advance MTH

A: 

have a look at the php extract function, it allows you to load an array of variables into the scope you want

http://nz2.php.net/manual/en/function.extract.php

ob_start();
extract($my_variables_array);
include $phpfile;
$output = ob_get_clean();
bumperbox
A: 

It sounds like dangerous stuff you're doing. Have you considered the case where the HTML/JS (which is inserted by the user of your CMS, I assume) contains strings matching your regex?

As for the scoping question: The function compact() can pack the current scope variables into an array and extract() can set them again. But be very very very cautious when using these functions. You might unexpectedly overwrite other variables you actually need.

function test($vars) {
    extract($vars);
    # The array might have contained the key 'vars', in which case
    # your function argument is now overwritten.
}
soulmerge
i'm aware of danger but I think that I got already everything figured out at level that will leave no risk. This part of feature would be only avaliable for admin usersI got also to experiment and read a little about solution provided in this answer and check if is it really what i need
MoreThanChaos
A: 

class autoloading might be worth looking at: http://www.php.net/manual/en/language.oop5.autoload.php

e4c5