We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7201
    • 6 Posts
    Hi everybody,

    I use modx on everyday basis. I like it and am really excited waiting for Revolution release. I am real fan of all that OOP stuff.

    However, I was a little disappointed seeing how library is organized. First, why class names start with lower case letters and why do we need suffix ’class’? I think it is understandable enough that in class folders we will find classes (so ’modaccesscontext.class.php’ -> ’ModAccessContext.php’).

    Next, I think it is already conventional for PHP to use library structure where class name represents where file is located in the library (like in Zend).

    So, proposal is to, for example, change ’modaccesscontext.class.php’ to: MODx/Access/Context.php with class name MODx_Access_Context.

    Simple autoloader example for this type of library structure:
    
    class Autoloader
    {
    
    	public static $dirs = array();
    
    
    	public static function addDirectory($dir){
    		self::$dirs[] = $dir . '/';
    	}
    	
    	
    	public static function register(){
    		spl_autoload_register(array(self, 'load'));
    	}
    
    
    	public static function load($name){
    		$fname = str_replace('_', '/', $name) . '.php';
    		foreach(self::$dirs as $dir)
    			if(file_exists($full_fname = $dir . $fname)) return require_once($full_fname);
    	}
    
    
        
    }
    
    


    
    // Usage:
    Autoloader::addDirectory('...core/model');
    Autoloader::register();
    $var = new MODx_Access_Context();
    
    // Include custom dirs at runtime
    Autoloader::addDirectory('...my/extra/dir');
    
    
    

      • 3749
      • 24,544 Posts
      I’ll leave the deeper questions to others, but I think I can answer the lower case question. It’s partly a legacy issue and partly due to the fact that class names are also file names in MODx and since file names are case-sensitive on some platforms but not others, it heads off hard-to-find problems by having all filenames and directory names all lowercase always.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: developer at Jan 16, 2010, 02:06 PM

        However, I was a little disappointed seeing how library is organized. First, why class names start with lower case letters and why do we need suffix ’class’? I think it is understandable enough that in class folders we will find classes (so ’modaccesscontext.class.php’ -> ’ModAccessContext.php’).

        Next, I think it is already conventional for PHP to use library structure where class name represents where file is located in the library (like in Zend).

        So, proposal is to, for example, change ’modaccesscontext.class.php’ to: MODx/Access/Context.php with class name MODx_Access_Context.
        This is all xPDO convention and MODx is developed with xPDO. They are technically model folders, not class folders, and they also include things like the O/RM map files. Auto-loading was not an option as xPDO 1.0 supported PHP 4. xPDO autoloading features will be added soon to the 2.0 releases, but it will likely retain the class/map file naming convention for compatibility reasons with existing xPDO models.