Re: Mixing eval() with include/require

From: Date: Sun, 24 Dec 2000 14:10:43 +0000
Subject: Re: Mixing eval() with include/require
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-31753@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. Defining the function in the include file works, of-course, but it isn't as flexible. 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 ... } 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.) 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. 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. -- Mark Rogers Remove the .news to reply by mail.

« previous php.general (#31753) next »