Last Updated: February 25, 2016
·
1.174K
· applebiz89

Traits are the best thing to happen to PHP

If you haven't made good use of traits you should start using them. There really couldn't be a better example of why to use traits than for this alone:

trait Singleton
{
    private static $_instance;

    public static function getInstance() {
        if (!self::$_instance instanceof self) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }
}

class Class_1
{
    use Singleton;
}

class  Class_2
{
    use Singleton;
}

That's at least 6 lines of code you never have to repeat to make a class Singleton...

1 Response
Add your response

Traits++
Singleton--

over 1 year ago ·