Note: this question is a bit long
I have a PHP-based system with Service-Dao-Model structure as follow:
Service Factory > Foo Service > Core Service > Abstract Service > Service Interface
Dao Factory > Foo Dao > Core Dao > Abstract Dao > Dao
Foo > Core Model > Abstract Model
Let me explain bit-by-bit. I try to centralize the codes first by define abstract functions in Abstract & Interface classes, then implement default behavior in Core Service, Core Dao and Core Model.
Then, the custom functions are written in Foo Service, Foo Dao and Foo Model. Last, Service Factory & Dao Factory is responsible to create object in Factory Pattern.
Other Information: all classes have log4php object attached in __construct().
In current development status, I loaded around 65 PHP classes in initialization phase. By adding timer (timed by microtime(true)), I found that PHP takes most time (0.02119s) on loading up the classes, next is converting DB result set to object values (0.00608s), last is DB query time (0.00223s). Can’t believe that class loading time is 10 times more than DB query time!
QUESTION: how to improve PHP initialization time on loading up classes?
Versions : PHP 5.2.16 , Connect to MySQL 5.1 localhost server via MySQLi PHP extension
UPDATE eAccelerator is pre-installed in shared hosting! Zend Engine v2.2.0 with eAccelerator v0.9.6.1
p.s. codes can be shared if needed.
I would assume you are using some kind of autoloading if the class loading times are so slow. Consider an approach similar to Symfony’s APC class loader:
http://symfony.com/doc/current/components/class_loader.html
https://github.com/symfony/ClassLoader
The idea behind it is that it caches the classpaths in APC so the include path for autoloading is not traversed for every request. Anyways I think there is some architechtural issue if the actual php loading is slower than the DB, even more so if its slower than the ORM layer…From my experience loading times are the other way around usually. Which parts of the PHP are slowest in particular. Another approach you can use is also an idea from symfony, where you have a deploy script which upon deployment merges all classes in one file, reducing file system calls, but i dont know if thats going to even be practical in your case, its definitely a speed improvement. Basically every time you have a file system operation its dead slow, so avoid it as much as possible..