__autoload($name)

2 views
Skip to first unread message

crash82

unread,
Sep 9, 2010, 7:13:00 AM9/9/10
to Professional PHP Developers
Hello,

I've been looking at autoload and was wondering your opinion on this
code...

<?php

define("BASE_PATH", "/var/www/project2", true);

ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/
Marketing/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/
Suporte/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/
Dados/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/
GeraDocXml/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/
Ngr/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/M/
Partner/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/
includes/config/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/
includes/nusoap/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/
includes/helpers/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/
includes/Smarty/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/
includes/pdf/");
ini_set("include_path", get_include_path() . ":" . BASE_PATH . "/
outsource/M/");

function __autoload($name){
switch($name) {
case 'bd':
$name = 'mysql_class';
break;
default:
break;
}
require $name.".php";

}

try {
$tempo = microtime();
$time = explode(" ", $tempo);
$tempo = $time[1] + $time[0];
$a = new Aquisicao();
$tempoa = microtime();
$time = explode(" ", $tempoa);
$tempoa = $time[1] + $time[0];


print "Aquisicao.php total = ".($tempoa-$tempo).PHP_EOL; //
Aquisicao.php is inside /M/

}catch(Exception $e){
print $e->getMessage() . PHP_EOL;
}
try{
$tempo = microtime();
$time = explode(" ", $tempo);
$tempo = $time[1] + $time[0];
$b = new Item();
$tempoa = microtime();
$time = explode(" ", $tempoa);
$tempoa = $time[1] + $time[0];


print "Item.php total = ".($tempoa-$tempo).PHP_EOL; // Item.php
is inside M/Marketing/
}catch(Exception $e){
print $e->getMessage() . PHP_EOL;
}


It takes less time to load Item.php then the time taken to load the
Aquisicao.php

Aquisicao.php total = 0.0142250061035
Item.php total = 0.00566506385803

/M/ has 63 files and dirctories
/M/Marketing/ has 13 files and directories

Any comments on this, does having small quantities of files in
directories helps performance of Include_path + autoload ?

Can autoload simplify memmory usage in the application by only loading
specific code ?
I think yes.

Robert Gonzalez

unread,
Sep 9, 2010, 1:11:13 PM9/9/10
to professi...@googlegroups.com
PHP has a default autoload stack and process it follows when a class name is called that has not yet been defined in the script. Making PHP find the file you want to load the class from is expensive, and the more work it has to do during the find, the longer it will take to process your code.

Generally I use a single static class method to handle my loading, and I always names classes along old-school "namespace" rules (a la Zend Framework) so the logic in loading is pretty stripped down and loads files pretty fast. If you are going to put a lot of searching and such in your loader expect your process times to get bigger.

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

crash82

unread,
Sep 9, 2010, 2:01:48 PM9/9/10
to Professional PHP Developers
Hi,

I see what you mean and I have to agree, but given my current file
organization at the moment I would require some that doesn't change
file names :S

Also another solution that I thought about was having a config array
with the location of the files.
$ClassArray[$name] = $path;

if (array_key_exists($name, $ClassArray)){
require($ClassArray[$name]);
}

A simple shell script would fill the array and the people could had
more class's during development,
this would free up memory usage to only required code, and simplify
the class loading using __autoload



On 9 Set, 18:11, Robert Gonzalez <robert.anthony.gonza...@gmail.com>
wrote:
> PHP has a default autoload stack and process it follows when a class name is
> called that has not yet been defined in the script. Making PHP find the file
> you want to load the class from is expensive, and the more work it has to do
> during the find, the longer it will take to process your code.
>
> Generally I use a single static class method to handle my loading, and I
> always names classes along old-school "namespace" rules (a la Zend
> Framework) so the logic in loading is pretty stripped down and loads files
> pretty fast. If you are going to put a lot of searching and such in your
> loader expect your process times to get bigger.
>

Robert Gonzalez

unread,
Sep 9, 2010, 2:23:36 PM9/9/10
to professi...@googlegroups.com
If you're bound by your current architecture then there isn't much you can do with this. All I can suggest, moving forward, is to architect your applications in a way that allows for logical autoloading of classes in some capacity.
Reply all
Reply to author
Forward
0 new messages