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.
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.
Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig
forget it.
I thnk its cleaner using the current loader class.
This is what I was going to use though:
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;
}
}
}
?>
Doesn't __autoload consume more memory at the outset?Daniel wrote:Does anyone think I should change the current loading system and replace it with an autoloader function?
The benefits of using __autoload far out way any overheads.starchy wrote:Doesn't __autoload consume more memory at the outset?
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"phpuk wrote:The benefits of using __autoload far out way any overheads.starchy wrote:Doesn't __autoload consume more memory at the outset?
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.
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 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.
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.Daniel wrote:Do you mean the zend naming convention?phpuk wrote: The use of __autoload encourages the use of strict naming conventions.
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.
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
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 ...
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.
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);
}
}
Phil.
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
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
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);
}
}
It will not work for your system/library classes as the class names are simply called Cart for example.
Phil
Who is online
Users browsing this forum: Amazon [Bot] and 5 guests