Traits Articles

Read the latest articles in the 'Traits' category.

PHP Traits

PHP 5.4 Trait Qwerks

Although the title may lead you to think otherwise; I actually really like Traits. I wish I had made the switch to 5.4 sooner! Bundle them with the shorter array syntax and I'm a happy PHP dev.

I recently experimented with Traits in a Laravel package. You can find the package here DatController

This blog is contains a couple of the not-so-obvious points of traits which I came accross.

You can not override trait properties

I think this is what I like the least about traits. You can not set a default property and have a class override it.

<?php
trait NewTrait {
	public $variable = 'default value';
}

class NewClass {
	use NewTrait;
	
	public $variable = 'override value';
}

I would love to be able to override trait properties. However, you could argue that it's best to use inheritence for these situations.