PHP 5.4 Trait Qwerks

Mitch

Warning - Old content ahead!

This page has been marked as a legacy post. This means it's quite old (from 2014) and probably out of date. Take it with a grain of salt!

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.

You can not use two traits that both require the use the same trait.

This one is a little more tricky to explain.

As an example say you have the following set up:

trait BaseTrait {
	public function base_method()
	{
	}
}

trait CreateTrait {
	use BaseTrait;
	public function create()
	{
	}
}

trait UpdateTrait {
	use BaseTrait;
	public function update()
	{
	}
}

trait ResourceTrait {
	use CreateTrait;
	use UpdateTrait;
}

Because both CreateTrait and UpdateTrait include the BaseTrait you will receive the following error:

Trait method base_method has not been applied, because there are collisions with other trait methods on ResourceTrait

Even though it's the exact same method it will create a collision.

That's all for now. I definitely want to use traits more in PHP but I do feel they are quite restrictive in some situations.

Spread the word

Share this article

Like this content?

Check out some of the apps that I've built!

Snipline

Command-line snippet manager for power users

View

Pinshard

Third party Pinboard.in app for iOS.

View

Rsyncinator

GUI for the rsync command

View

Comments