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 3 of 3
  1. #1
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts

    Spl_autoloading Cascade Issue

    Hi, I am having a problem I am not sure what to think. I am using spl_autoload_register to name multiple autoload functions of my own, this is my test:

    PHP Code:


      
    function auto_class($class) {  include_once "classes/".$class.".php"; }
      function 
    auto_iface($iface) {  include_once "interfaces/".$iface.".php"; }

      
    spl_autoload_register("auto_class");
      
    spl_autoload_register("auto_iface"); 
    This is working as I thought, with the autoload trying first classes then interfaces. The problem is, it always complains when loading an interface!

    "Warning: include_once(classes/file.php) [function.include-once]: failed to open stream: No such file or directory in.. etc"

    Understandable, since it tries to include_once it as a class first. I can suppress these errors with @ and I wouldn't have display_errors on for a production site but it will fill up my error log with junk mail, and I don't like have warnings! I am thinking, am I using autoload wrong? I would like to require_once, but that will stop my script for any but the first. Do I have to make my own logic to stop this from happening? If so, is file_exists a good method? I heard it can be unreliable, but maybe not in this case.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Simply use file_exists first. If it exists, issue a require, otherwise ignore it and let the method return void.

  • Users who have thanked Fou-Lu for this post:

    Custard7A (11-11-2012)

  • #3
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    M'kay, it's doing what I want now that I'm checking the file first. It actually seemed kinda redundant having multiple functions, in this situation.
    PHP Code:


      
    function auto_load ($obj) {

        
    $incl ini_get("include_path")."/";
        
    $path = array( => "classes/"=> "interfaces/" );
        
    $file $obj.".php";

            if(
    file_exists$incl.$path['1'].$file )) { require_once $path['1'].$file; }
        elseif(
    file_exists$incl.$path['2'].$file )) { require_once $path['2'].$file; } 

      }

      
    spl_autoload_register("auto_load"); 
    I'll be playing around with this, seeing how streamline I can make it. Thanks for the tip Fou-Lu.


  •  

    Posting Permissions

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