Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-52634

Policy: magic __get and __set

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.1
    • Fix Version/s: 3.1
    • Component/s: Policy
    • Labels:
      None
    • Affected Branches:
      MOODLE_31_STABLE
    • Fixed Branches:
      MOODLE_31_STABLE

      Description

      I'm opening this issue to discuss how are should use __set and __get (same should be applicable to __isset and __unset) like any lazy developer I used to like magic methods and I used them a lot to avoid boilerplate code and to reduce maintenance. They are nice when writing code but not nice when reading code from others.

      They have some negative points:

      1. Resulting code is not as readable as normal function names
      2. Interfaces like $obj->attr when you know you are not dealing with a stdClass are confusing; they are not straight forward to deprecate and, may result in future conflicts if we change the class interface. This is worst when they are public interfaces as we need to maintain backwards compatibility
      3. Not ideal during development: IDE autocompletion not working, shared php-doc block... (this last point might be considered good though)
      4. Slower than normal functions, depending on where they are implemented the difference is significant, I don't think we should use the premature optimisation thing as an excuse here. Performance comparison: http://www.garfieldtech.com/blog/benchmarking-magic

      As examples of this we have modinfolib, where we abuse the functionality and results in a significant performance impact as it is a widely used lib in some components.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  23/May/16