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

Clarify a little bit the usage of stdClass() and (object)[]

    XMLWordPrintable

    Details

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

      Description

      Following a discussion @ Moodle Dev Chat:

      https://moodle.org/local/chatlogs/index.php?conversationid=23025

      This is about to try to improve/clarify a little bit the use of both new stdClass() and (object) [] constructors to get an "object" (stdClass) instance. And apply it to (the end of):

      https://docs.moodle.org/dev/Coding_style#Classes

      This is the proposal to start the discussion about:

      a) By default, new stdClass() should be used, and not the empty casting (object) [] alternative. Note a backslash may be needed under namespaced stuff. And using use stdClass; is highly discouraged.

      b) phpdocs should also use stdClass and not object or any other alternative. This is already @ https://docs.moodle.org/dev/Coding_style#Types.

      c) When objects are being created with properties, it's allowed to use $obj = (object) ['data' => 'value'];. Of course it's perfectly valid to use $obj = new stdClass(); $obj->data = 'value';.

      d) When objects are being created without properties, but in a context of other objects being created with properties as specified in c) (say a data provider or a big structure... ) then it's allowed to use the empty casting (object) []. Exclusively for readability in a context.

      e) Any other use of empty casting apart from the exception in d) is not allowed.

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            stronk7 Eloy Lafuente (stronk7)
            Participants:
            Component watchers:
            Marina Glancy, Eloy Lafuente (stronk7)
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: