Last Updated: February 25, 2016
·
1.328K
· 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 ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside