1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using Traits

Traits were introduced in PHP 5.4 and are a great way to improve DRY coding.
Categories:
  1. Traits in PHP 5.4.png
    This code has me excited. Most XenWord classes reuse the same code for calling the options in WordPress database.

    PHP:
    1. class Post {
    2.     protected $options;
    3.  
    4.     /**
    5.     * Post constructor.
    6.     */
    7.  
    8.     public function __construct() {
    9.         $this->options = get_option( 'xenword_options' );
    10.     }
    Traits were introduced in PHP 5.4 and so repetitive code can be moved into a trait and called with the use statement. The following video shows the replacement of the above code with use Options.



    It's a great way to speed up coding. In the future, new options can also be added to the trait file.

    About Author

    LPH
    LPH uses PHPStorm, Atom, MAMP-Pro, Xdebug, FileZilla and plenty of patience to code XenWord. He is not an expert by any stretch of the imagination.
    robru likes this.

Comments

To make a comment simply sign up and become a member!
  1. Gracie
    I noticed you pulled traits from the latest. Is there a reason why you stopped?