PHP

Traits – PHP5.4

Since it is new years eve, I don’t want to start a complete new chapter.
So I will write one last post this year with some more general topic.
And yes – I find it so interesting that I have to mention it in an own post 🙂

Traits

With PHP5.4 approaching fast, it will probably be the standard sometime in 2012.
It will have one major feature that we will all going to love: oop5.traits.

What are they?
The above link describes it pretty good. They are some kind of behavior – like the one we attach to models right now. Only way more flexible and powerful.
Every class can then use those behaviors.

trait Hello {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait World {
    public function sayWorld() {
        echo 'World!';
    }
}

trait HelloWorld {
    use Hello, World;
}

class MyHelloWorld {
    use HelloWorld;
}

$Foo = new MyHelloWorld();
$Foo->sayHello();
$Foo->sayWorld();

The result would be Hello World!

Imagine what would be possible with Controllers, Components, Shells, and other classes that can now not only extend parent classes but also use traits.

Cool things you can do with them

The above link contains a good set of examples.
I will outline the most interesting ones:

public function sayWhere() {
    echo __CLASS__; // same with __FILE__ etc
}

As of right now objects that are extended by some subclass would always return the wrong class, because inside a class those magic constants always relate to the current file (and therefore often the parent class defining it).
With traits the magic is endless – they will make it possible to inject functionality that overcomes that deficiency:

trait sayWhere {
    public function whereAmI() {
        echo __CLASS__;
    }
}

class Hello {
    use sayWHere;
}

class World {
    use sayWHere;
}

$One = new Hello;
$One->whereAmI(); //Hello

$Two = new World;
$Two->whereAmI(); //World

Another neat feature is, that we can override specific methods if needed – both ways:

class Base {
    use SayBye;

    // we want this method to be overridden by the trait to do some extra stuff with it
    // can only be done in the inheriting class, though
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

trait SayBye {
    // this method will be be overridden by the class method to do some extra stuff with it
    // can only be done in the inheriting class, though
    public function sayBye() {
        echo 'Bye ';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;

    public function sayBye() {
        echo parent::sayBye();
        echo 'World!';
    }
}

$Foo = new MyHelloWorld();
$Foo->sayHello(); //Hello World!
$Foo->sayBye(); //Bye World!

Update 2012-02-24: Interesting Link

There is an interesting page about the potential dangers of those new PHP5.4 features you might want to read before starting to write cutting-edge code 🙂

0.00 avg. rating (0% score) - 0 votes

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.