STATUS ÜBERPRÜFEN
I AM LISTENING TO
|
  • ♩
  • ♪
  • ♫
  • ♬
  • ♩
  • ♪
  • ♫
  • ♬

Register classes and traits for autoloading

LAST MODIFIED: 8. März 2025
8. März 2015
.SHARE

Table of Contents

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

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.

class{
 use traitOne, traitTwo;
}
Let’s Talk!

Looking for a reliable partner to bring your project to the next level? Whether it’s development, design, security, or ongoing support—I’d love to chat and see how I can help.

Get in touch,
and let’s create something amazing together!

RELATED POSTS

FAQ What is the best free open source way to back up IMAP email? For most people, mbsync (isync) pulls IMAP into a local Maildir reliably on Linux, macOS and Windows via WSL, and imap-backup does the same job with an easier cross-platform setup and no config-file syntax to learn. Pair whichever you choose with […]

Your keyboard and pointing device are the two tools you touch more than anything else on your desk — more than your monitor, more than your editor. A developer types for a living, and after enough years of it, the wrong keyboard and mouse stop being a minor annoyance and start being a wrist problem […]

If you’re a developer in Europe who works from home and runs a homelab, your chair and desk are the two pieces of hardware you touch more than any server, keyboard or monitor. You’ll spend thousands of hours in that seat, and a scary amount of expensive gear is going to live on that desk. […]

Alexander

I am a full-stack developer. My expertise include:

  • Server, Network and Hosting Environments
  • Data Modeling / Import / Export
  • Business Logic
  • API Layer / Action layer / MVC
  • User Interfaces
  • User Experience
  • Understand what the customer and the business needs


I have a deep passion for programming, design, and server architecture—each of these fuels my creativity, and I wouldn’t feel complete without them.

With a broad range of interests, I’m always exploring new technologies and expanding my knowledge wherever needed. The tech world evolves rapidly, and I love staying ahead by embracing the latest innovations.

Beyond technology, I value peace and surround myself with like-minded individuals.

I firmly believe in the principle: Help others, and help will find its way back to you when you need it.