Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Dynamic function generation using include()?

    I was wondering what the best approach to my problem is. Basically I need to dynamically generate a portion of a function (not its output), with that portion coming from say an external file. So instead of:

    Code:
    function test(){ 
    $x="whatever";
    for ($i=0; $i<5; $i++){
    echo date("l \\t\h\e jS");
    echo '<b>Example</b>';
    }
    }
    I need to have the code in red come from an external file (mostly output codes), and dynamically included as part of the function code:

    Code:
     
    template1.php:
     
    <?
    echo date("l \\t\h\e jS");
    echo '<b>Example</b>';
    ?>
     
    Modified function:
     
    function test(){
    $x="whatever";
    for ($i=0; $i<5; $i++){
    include("template1.php");
    }
    }
    Now, the modified function above works, but I know it's very inefficient (ie: including the same piece of code 5 times), not to mention perhaps poorly thought out. Are there more elegant ways to dynamically generate a portion of a function from an external file, especially inside a for loop? The task I'm trying to accomplish is basically to create user swappable templates for the code portion in red of the function.

    Thanks,
    Last edited by WA; 11-29-2006 at 12:57 PM.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,859
    Thanks
    22
    Thanked 157 Times in 148 Posts
    Is there only the one file: template1.php,

    Or are there many different files: template1.php, template2.php, template3.php, template4.php, etc...?

    My guess is there is only one, because you are not passing the template file name into the function?
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Nope, there will be many, as it will be user customizable at the top of the script. So something like:

    Code:
    $templatepath="template2.php";
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    If you need to dynamically include other files then just pass the name of the include file into the function.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Why not use other functions? You are using the include as if it was a function...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Why not use a wannabe lambda construct:
    http://us3.php.net/create_function

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Wow, so many old faces

    Since it appears to be used as templates for output, perhaps using a specially defined eval function would work best for you? Something simple, just read the file in, parse the file, and return the output? You can use that in a loop as well, and I think it would work quite well so long as you are using it as output templates.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Any reason you don't just enclose the code in the includes in a function wrapper so you can include once and just call the function in the loop? Here's a function that'll wrap them for you, but whether it'll be any faster than just including in the loop, meh.

    PHP Code:
    <?php
    function inclose($filename)
    {
            
    $code file_get_contents($filename);
            if (!
    $code)
            {
                    throw new 
    Exception('oh noes');
            }
            
    $code preg_replace('/(?:\<\?(?:php)?|\?\>)/xms'''$code);

            return 
    create_function(''$code);
    }

    $func inclose('template1.php');
    for (
    $i 0$i 5; ++$i)
    {
            
    $func();
    }
    ?>
    Edit for another even slower version with enterprisey caching:

    PHP Code:
    abstract class Inclosure
    {
            public static function 
    inclose($filename)
            {
                    static 
    $func_repo;
                    if (!isset(
    $func_repo[$filename]))
                    {
                            
    $func_repo[$filename] = Inclosure::load_file($filename);
                    }
                    return 
    $func_repo[$filename];
            }

            private static function 
    load_file($filename)
            {
                    
    $code file_get_contents($filename);
                    if (!
    $code)
                    {
                            throw new 
    Exception('oh noes');
                    }
                    
    $code preg_replace('/(?:\<\?(?:php)?|\?\>)/xms'''$code);

                    return 
    create_function(''$code);
            }
    }

    $func Inclosure::inclose('myinc.php');
    for (
    $i 0$i 5; ++$i)
    {
            
    $func();

    Last edited by ralph l mayo; 11-30-2006 at 06:43 PM. Reason: oops

  • #9
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,859
    Thanks
    22
    Thanked 157 Times in 148 Posts
    it seems like your best bet is to open some or all of your files outside of the function and for loop, and dump them into variables.

    Store your template code in text files, so template1.txt could be:

    echo date("l \\t\h\e jS");
    echo '<b>Example</b>';
    Let's say $template is an array of your template file names,

    PHP Code:
    $template = array('template1.txt','template2.txt','template3.txt','template4.txt');

    foreach (
    $template as $val) {
           
    $template_contents[] = file_get_contents('/path/to/templates/'.$val);

    then use eval() inside your function, and pass the template file contents as an argument....so this function:

    PHP Code:
     function test($the_code){ 
     
    $x="whatever";
     for (
    $i=0$i<5$i++){
     eval(
    $the_code);
     }
     } 
    would be called like:

    PHP Code:
     test($template_contents[2]);  // To pass the file contents of template3.php into your function 
    Edit: And if the user is selecting a template via GET or POST, then all you need to do is:

    PHP Code:
    $template = array('template1.txt','template2.txt','template3.txt','template4.txt');
     
     if (
    in_array($_POST['template_name'],$template))
        
    $template_contents file_get_contents('/path/to/templates/'.$_POST['template_name']);
     
     
    test($template_contents); 
    And use the same function as above.
    Last edited by chump2877; 11-30-2006 at 02:02 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #10
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks guys for all the responses, and sorry for the delay in posting back. I haven't had a chance to revisit this issue until now. Chump2877, your solution looks like it could be what I need, thanks. I'm a little hesistant of using eval() though for security reasons, though I don't see any issues right out of the bat on the above.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #11
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,859
    Thanks
    22
    Thanked 157 Times in 148 Posts
    Another thing I was thinking, too, if you're worried about storing code in flat files, and the potential security issues with that -- then store the code in a MySQL database.

    It might speed up your script too, since there will be no opening up and reading of files. Just database queries.

    Food for thought.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #12
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks Chump2877, I've been thinking about that as well. mySQL certainly would make things more secure, especially with such a setup.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •