Re: Mixing eval() with include/require
| From: | Rasmus Lerdorf | Date: | Sun, 24 Dec 2000 17:03:25 +0000 |
| Subject: | Re: Mixing eval() with include/require | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31758@lists.php.net to get a copy of this message | ||
> Rasmus Lerdorf wrote
> > I really don't understand why you don't just use an include file with
> > functions and then call the function inside the loop. This is exactly
> > what functions are for. Including a file repeatedly inside a loop, or
> > calling eval() repeatedly is not a good approach.
>
> That's what I'm trying to do! I want to define the function based on the
> content of an external file, then call that function from within the loop.
This external file has PHP code in it? If so, whoever is writing this
file will need to have a clue anyway.
> Basically, the include file is being written by a third party who cannot be
> assumed to know a huge amount of PHP. What I want is a function along the
> lines of:
> function TestFnX() {
> global $somevar;
> // Do some stuff
> ...
> // The lines from the include file go next...
> ...
> // Do some more stuff
> ...
> }
I still don't see why you don't just put those lines from the include file
inside a function. Then only include that file once before the TestFnX()
definition and call the function inside the TestFnX() function.
> Defining the whole function in the include file requires that the third
> party doesn't change stuff they shouldn't. (I could call a nested function,
> but even then they have to make sure they get function parameters and global
> var declarations correct.)
There is no need to define the whole function there. Just put whatever
lines are in the file inside the function.
> Including the code via include() means that the lines are read in each time
> the function is called, not just when the function is first parsed.
No, include the file before the function definition.
> I had hoped that using eval() to generate the function (ie eval('function
> TextFnX() {..etc..}')) would get around this, but it doesn't - which is
> where this thread started.
>
> I suspect that something like:
> eval ('function TestFnX(){' .
cat tesffnx.inc .
> '}')
> .. would work, but I haven't tried it (only just thought of it). When I get
> back into the office after Christmas I might give it a go.
This makes absolutely no sense to me.
Make your tesffnx.inc file look like this:
function blah() {
...PHP code...
}
then do:
include 'tesffnx.inc';
function TestFnX() {
blah();
}
If you trust them to write PHP code in tesffnx.inc then I don't understand
why you can't have them start the file with a function definition. They
are no more likely to screw that up than they are to screw up the PHP code
itself.
-Rasmus