STATUS ÜBERPRÜFEN
I AM LISTENING TO
|

Register classes and traits for autoloading

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

Font licensing is wild. Proxima Nova — one of the most-used typefaces on the entire web — will run you $65 per style, and a full family license easily clears $300. Futura? Brandon Grotesque? Circular (yes, Spotify’s font)? We’re talking hundreds of dollars before you’ve typed a single character. For personal projects, indie dev work, […]

So you want to set up email on a subdomain. Maybe you’re trying to route support@help.yourdomain.com to your helpdesk, or you want newsletters@mail.yourdomain.com to run through your ESP without torching your main domain’s reputation. Whatever the reason, you’ve landed on the right page. MX records for subdomains are one of those DNS topics that seem […]

If you have ever embedded an audio player on a podcast site, a music portfolio, or a media archive, you have probably noticed that pretty rendered waveform behind the playhead. Libraries like Wavesurfer.js and Peaks.js can draw those visuals on the client, but decoding a 60-minute MP3 in the browser is slow, memory-hungry, and unreliable […]

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.