Categories: DevelopmentPHP

Clean up your toolbox with PHP traits …

Single inheritance has often been the limitation for PHP. This means that a class can only inherit from one other class. Often classes share the same methods and it would be beneficial to allow reuse and prevent duplication.

In PHP 5.4 a new feature was added, known as Traits. A Trait is like a Mixin, allowing to use / mix classes into existing classes. This means code reduction and less duplication.

Example of a Trait

  1. trait Sharable {
  2.  
  3.   public function share($item)
  4.   {
  5.     return 'share this item';
  6.   }
  7.  
  8. }

This trait can than be used in other classes:

  1. class Post {
  2.  
  3.   use Sharable;
  4.  
  5. }
  6.  
  7. class Comment {
  8.  
  9.   use Sharable;
  10.  
  11. }

Both classes can now use the Shareable trait.

  1. $post = new Post;
  2. echo $post->share(''); // 'share this item'
  3.  
  4. $comment = new Comment;
  5. echo $comment->share(''); // 'share this item'

The benefit is less code duplication. The drawback is, that not all used methods are visible within the source-code of the class. So before moving a method to a trait, make sure that you will actually reuse it ;)

I have been restructuring huge amounts of code over the past months and traits are helping to keep things far more organized.

In combination with __autoload its a real nice way to cleanup your code toolbox.

Happy coding
Alex

  1. Traits
  2. Autoload
Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

1 day ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago