Uploaded image for project: 'Moodle Community Sites'
  1. Moodle Community Sites
  2. MDLSITE-3777

Document the allowed use of "traits" before it gets out of hand.

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Component/s: Coding style
    • Labels:

      Description

      php traits are a php 5.4 feature to allow multiple inheritance. Many of us had never heard of them in php before MDL-41266.

      http://au2.php.net/traits

      This policy issue is to agree on the allowed uses of traits in Moodle before they get out of hand. They look susceptable to abuse and misuse and several concerns were raised during integration of MDL-41266.

      Some things should be clearly forbidden.

      The definition of a trait should be complete. This means it should not rely on functions declared outside of itself without declaring those functions abstract or explicitly importing them from another trait.

      Forbidden example:

      trait A {
      static function B()

      { self::C(); }

      }

      Renaming constructors using AS is forbidden (and breaks php).

      Something that IMO should be forbidden is implementing multiple behaviours in a single trait. A trait should define a single behaviour or feature - combining multiple behaviours or features in the same trait prevents the features from being used separately. This complicates the trait and makes it less reusable. Any class named "helper, util or stuff" smells of a dumping ground for common code which is not what traits are for.

      Reading:
      http://blog.ircmaxell.com/2011/07/are-traits-new-eval.html
      http://www.sitepoint.com/php-traits-good-or-bad/

        Attachments

          Activity

            People

            • Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: