#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Post Autoload Class/Helper files Issue


    Long time reader, not a first time user. Been out of the game for a few years, so I'm glad to be back!


    For a project I'm working on I must include all files within a directory. These files contain procedural (helper) functions and classes.

    So Far this is what i've come up with:
    PHP Code:
    <?php
    function scanlib($dir

        
    $root scandir($dir); 
        foreach(
    $root as $value
        { 
            if(
    $value === '.' || $value === '..') {continue;} 
            if(
    is_file("$dir/$value")) {$result[]="$dir/$value";continue;} 
            foreach(@
    scanlib("$dir/$value") as $value
            { 
                
    $result[]=$value
            } 
        } 
        return 
    $result


    function 
    my_autoload(){
        
    // chdir('../libnew');
        // print_r(@find_all_files('../libnew'));
        
    $include_file = array();
        
    $included_files = array();
        foreach(@
    scanlib('../libnew') as $file){
            if(
    is_file($file)){ $include_file[$file] = $file; }
            foreach(
    $include_file as $k){
                if(!
    array_key_exists($k$included_files)){
                    
    $included_files[$k] = $k;
                }
            }
        }
        foreach(
    $included_files as $inc){
            include_once(
    $inc);
        }
    my_autoload();
    The error I receive is:
    Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 67108864 bytes) in /home/***/project/lib/myapi.php on line 31
    The reason for this is so that I don't have to create a huge list of files to include, as I plan to also modify this to include a plugin feature.

    Any suggestions, and help would be a preciated. Please do not tell me to use a framework like zend,CI,Drupal,CakePHP, etc. If I wanted to use them, I would. I'm not reinventing the wheel, I'm merely finding my own way.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    first of all, I don't see what this has to do with autoloading or lazy loading. This is the exact opposite: You preload all scripts you have, regardless of whether you need them or not.

    67,108,864 bytes is 67 MBytes. Does it really surprise you that you need more than 67 MBytes to load your complete library?

    I don't think this whole approach is a good idea. You don't even have a filter for PHP scripts. You just dump any file from any subfolder into your main script. This is calling for trouble, and it's a big security risk. Anybody who manages to upload a script into the directory structure will get the script execution for free.

    You should at least only include ".php" files.
    Last edited by Jacques1; October 5th, 2013 at 07:52 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Thank you for your reply Jacques1. I couldn't really think of what to use for a title and I did try to change it in the end . That was what I had rewritten so far before I was stumping myself.
    I originally started out with something like this for an idea:
    PHP Code:
    $included_directories         = array("api");
        
    $excluded_directories         = array(".","..");
        
    $registered_extensions        = array(".php",".api",".func",".infc",".class",'.db');
        
    $unregistered_extensions    = array();

        foreach(
    $included_directories as $directory){
            
    // print $directory."<br>\r\n";
            
    $current_dir scandir($directory);
            foreach(
    $current_dir as $subdir){
                print 
    "<pre>"print_r($subdir);
            }
        } 

    I plan on limiting it to a specific folder and only include files that have the allowed extensions.
    Do you think a cached file with all the includes would be a better solution?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Putting the files into separate folders and loading only the folders you need is the only halfway smart option you have.

    The underlying problem is that you're obviously dealing with a huge collection of loose function. That will always lead to trouble, no matter what you do. Consider moving to object-oriented programming, which allows you to structure your code. Classes also do have autoloading. I mean actual autoloading: If a class doesn't exist, PHP takes care of loading the script.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    I'm starting to realize that this will be a difficult task. Heh.

    I was always going to include an autoload for classes. That's a given. My problem stems from the fact that I am trying to dynamically include helper functions that provide easier accessibility. My underlying reason for trying this technique was to learn a way of creating a plugin like system.

    I think, I think that I'm over-thinking things.

    As of writing this, I'm seeing things fall into place within my mind, at least in regards to my current problem. The plugin idea is still befuddling me. Any idea's, suggestions?

IMN logo majestic logo threadwatch logo seochat tools logo