SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Including a class for autoload.php

    I am breaking down a MVC framework created from a tutorial and I'm confused by the code below and the process. I don't know why they created a class in a separate file and not just use the code in the first file. Here is the code from the first file named utility.php
    Code:
    function __autoload($class_name)
    {
    	$inc = null;
    	require_once '../library/autoload.php';
    	$inc = new autoload($class_name);
    }
    Here is the code from the autoload.php

    Code:
    <?php
    
    class autoload
    {
    	public function __construct($class)
    	{
                /*The next line is also in the utility.php file. Each occurance is to set up
                 * 
                 */
    		$paths = parse_ini_file('../application/config/paths.ini.php', true);
    
    		foreach($paths['include_paths'] as $path):
    			$class = str_replace('_', '/', $class);
    			$file = $path.$class.'.php';
    			if(file_exists($file)):
    				include $file;
    				return;
    			endif;
    		endforeach;
    
    		if(strstr($class, 'Controller')):
    			throw new gException('Class \''.$class.'\' could not be loaded by the autoloader');
    		endif;
    	}
    }
    Maybe I'm not understanding the what the code in the utility.php file is doing.
    Thanks in advance for any help.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It appears to be using an object just for the sake of using an object...
    When doing this would be just fine:
    Code PHP:
    function __autoload ( $class )
    {
        $paths = parse_ini_file( '../application/config/paths.ini.php', true );
     
        foreach ( $paths['include_paths'] as $path ) {
            $file = $path . str_replace( '_', '/', $class ) . '.php';
            if ( !file_exists( $file ) ) continue;
     
            include $file; break;
        }
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agree, you could use a class or a function, but both is a little silly.
    You should also check out http://php.net/manual/en/function.sp...d-register.php
    If you need a third party lib that wants to autoload as well, then __autoload is not much good.

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for replying.
    logic_earth: Thanks for clarifying what I was thinking. The entire application seemed a bit heavy to me so as I'm breaking this down to learn MVC I'll keep a look out for this type of practice.

    hash: Thanks for the link. I'll keep that in mind for future study. Right now I'm feeling a bit overwhelmed just breaking this code down. That is I have three tutorials that approach MVC framework in different ways and I'm breaking down each parallel to each other. My ultimate goal is to train my mind to think about programming from scratch. e.g. This is what I want the program to do and this is what I need from php to accomplish it.
    Cheers!


Bookmarks

Posting Permissions

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