PLDB
Languages Features Calendar About Lists Add Language
GitHub icon

Traits

Traits

Traits are a language feature.

Sometimes referred to as mixins. In computer programming, a trait is a concept used in object-oriented programming, which represents a set of methods that can be used to extend the functionality of a class.

Languages with Traits include PHP

Example from PHP:

// The template trait TSingleton { private static $_instance = null; private function __construct(){} // Must have private default constructor and be aware not to open it in the class public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } } class FrontController { use TSingleton; } // Can also be used in already extended classes class WebSite extends SomeClass { use TSingleton; }

Article source

PLDB - Build the next great programming language. v5.0.0 - Acknowledgements · Email · GitHub