When projects are getting bigger, its always nice to split things up and prepare parts of your code for general reuse.
PHP Traits (PHP 5.4+) are a nice way to do this. Adding multiple Traits or classes, requires you to make sure all are being included.
Here is a nice way to register both with the __autoload function
1 2 3 4 5 6 7 8 9 10 11 12 13 |
spl_autoload_register(function($className) { $fileName = __DIR__.'/class/class.' . $className . '.php'; if (file_exists($fileName)) { include $fileName; } }); spl_autoload_register(function($traitName) { $fileName = __DIR__.'/trait/trait.' . $traitName . '.php'; if (file_exists($fileName)) { include $fileName; } }); |
Traits are loaded when you specify their use in a class.
1 2 3 |
class{ use traitOne, traitTwo; } |