Page 1 of 1

PHP5 Autoload

Posted: Mon Jun 01, 2009 8:04 am
by Daniel
Does anyone think I should change the current loading system and replace it with an autoloader function?

Re: PHP5 Autoload

Posted: Mon Jun 01, 2009 11:26 am
by Qphoria
I use it in 0.x along with my dynamic locator create function. Works pretty well and removes the need for a static locator file with hardcoded class paths.

Re: PHP5 Autoload

Posted: Mon Jun 01, 2009 5:06 pm
by JNeuhoff
Are you talking about the __autoload?

In general, we need a mechanism where a 3rd party contribution can extend the functionality of an Opencart core class. For example, the Export/Import contribution modifies the menu.php, the Category Descriptions contribution modifies the category.php for the the backend admin and the frontend, etc.

The issue becomes tricky when 2 contributions modify the same core class.

Re: PHP5 Autoload

Posted: Thu Jun 04, 2009 6:04 am
by phpuk
You could implement some form of class overloading or inheritance so the bolt on modules simply extend the core classes.

This would need some analysis done on the current classes as some are declared as final classes.

Re: PHP5 Autoload

Posted: Thu Jun 04, 2009 9:02 am
by Daniel
forget it.

I thnk its cleaner using the current loader class.

This is what I was going to use though:

Code: Select all

<?php
final class Loader {
	protected $data = array();
	
	public function addFile($class, $file) {
		$this->data[$class] = $file;
	}
	
	public function addDirectory($directory, $prefix = '') {
		$directories = glob($directory . '*');
		
		foreach ($directories as $directory) {
			$data = pathinfo($directory);

			if (is_dir($directory)) {
				$this->addDirectory($directory . '/', $prefix . $data['basename']);
			} 
			
			if ((is_file($directory)) && ($data['extension'] == 'php')) {
				$this->data[preg_replace('/[^a-zA-Z0-9]/', '', strtolower($prefix) . $data['filename'])] = $directory;
			}
		}
	}
	
	public function load($class) {
		if (isset($this->data[strtolower($class)])) {
			return require($this->data[strtolower($class)]);
		} else {
			return FALSE;		
		}
	}
}
?>

Re: PHP5 Autoload

Posted: Thu Jun 04, 2009 2:04 pm
by removed331062
Daniel wrote:Does anyone think I should change the current loading system and replace it with an autoloader function?
Doesn't __autoload consume more memory at the outset?

Re: PHP5 Autoload

Posted: Sun Jun 14, 2009 9:38 am
by phpuk
starchy wrote:Doesn't __autoload consume more memory at the outset?
The benefits of using __autoload far out way any overheads.

The use of __autoload encourages the use of strict naming conventions.

I use __autoload as it can seriously benefit the performance of a script. If you don't need a class it simply isn't loaded.

There is no performance penalty to pay. In fact, there are in most cases performance improvements.

Phil.

Re: PHP5 Autoload

Posted: Sun Jun 14, 2009 7:19 pm
by Daniel
phpuk wrote:
starchy wrote:Doesn't __autoload consume more memory at the outset?
The benefits of using __autoload far out way any overheads.

The use of __autoload encourages the use of strict naming conventions.

I use __autoload as it can seriously benefit the performance of a script. If you don't need a class it simply isn't loaded.

There is no performance penalty to pay. In fact, there are in most cases performance improvements.

Phil.
"The use of __autoload encourages the use of strict naming conventions"

Do you mean the zend naming convention?

The zend naming convention is not good. In all the OOP languages out there I have never seen anything so bad. The zoned naming convertion will be moot once namespaces are out in php 5.3.

Re: PHP5 Autoload

Posted: Mon Jun 15, 2009 3:35 am
by removed331062
I think this is one of those it's-good-it's-bad scenarios. Depending on what you use the magic functions for, it may or may not be great. So for a small shopping cart, it probably isn't a problem and if anyone's operations expands to an immense level, they would probably migrate to a different custom paid cart anyway with paid support, service, etc. Personally, I prefer to code a little bit more than to rely on __autoload.

Re: PHP5 Autoload

Posted: Mon Jun 15, 2009 4:19 am
by Qphoria
Daniel wrote:
phpuk wrote: The use of __autoload encourages the use of strict naming conventions.
Do you mean the zend naming convention?

The zend naming convention is not good. In all the OOP languages out there I have never seen anything so bad. The zoned naming convertion will be moot once namespaces are out in php 5.3.
I don't think it means zend. It just means that using autoload means that the name of the class is assumed to be the name... or part of the name of the file.

Like $cart would be "cart.php"
depending on your design. but prevents things like
one guy making:
my_product_class.php

and another guy making:
my.cart.class.inc

Re: PHP5 Autoload

Posted: Mon Jun 15, 2009 5:34 am
by phpuk
Daniel, Qphoria,

Exactly right Qphoria.

Daniel, You can continue to use your current naming conventions (which are the same as mine) or Zend, PEAR or whatever.

In the case of OpenCart you would have a class called ControllerAccountAccount the __autoload could be used explode this and convert to lower case to produce
$class[0] = controller . '/'; // Add the slash
$class[1] = account . '/'; // Add the slash
$class[2] = account . '.php'; // Add the .php extension

Then check to see if a file exists in controller/account/account.php if so it then load it and registered the class name ControllerAccountAccount for example:
$className = new $className which translates to $ControllerAccountAccount = new ControllerAccountAccount .

A VERY VERY quick example ...

Code: Select all

function __autoload($className) {
    if(substr($className,0,10) === 'Controller'){
        $dir = 'controller/';
        $file = substr($className,10,strlen($className));
    } elseif(substr($className,0,5) === 'Model') {
        $dir = 'model/';
        $file = substr($className,5,strlen($className));
    }
	if (file_exists($dir . strtolower($file) . '.php')) {
		include($dir . strtolower($file) . '.php');
        $className = new $className;
	} else {
		die('ERROR! - Class can not be loaded ' . $dir . ' ' . $file . ' ' . $className);
	}
}
It is down to personal preference but I find it cuts down on coding and adds flexibility when creating addons as you can in most cases simply drop them in and away you go.

Phil.

Re: PHP5 Autoload

Posted: Tue Jun 16, 2009 3:33 am
by phpuk
An __autoloader that would work for OpenCart with a few changes to class naming standards.

For example the result of a call to this __autoload function with a class called ControllerAccountAccount would be converted to controller/account/account.php

Code: Select all

function __autoload($className) {
    preg_match_all('/[A-Z][^A-Z]*/',$className,$results);

    for ($i = 0; $i < sizeof($results[0]); $i++) {
        $dir .= strtolower($results[0][$i]) . '/';
    }

    $dir_file = substr($dir,0,strlen($dir)-1).'.php';

	if (file_exists($dir_file)) {
		include($dir_file));
        $className = new $className;
	} else {
		die('ERROR! - Class can not be loaded ' . $dir_file . ' ' . $className);
	}
}
This would work if your naming conventions are the same as mine and are strict and consistent throughout.

It will not work for your system/library classes as the class names are simply called Cart for example.

Phil