• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

Using Traits

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:
class Post {
    protected $options;

    /**
    * Post constructor.
    */

    public function __construct() {
        $this->options = get_option( 'xenword_options' );
    }

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.
  • Like
Reactions: robru

Blog entry information

Author
LPH
Views
2,245
Comments
1
Last update

More entries in Technology

More entries from LPH

Top