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

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


    • Icon: Task Task
    • Resolution: Fixed
    • Icon: Low Low
    • Coding style
    • None

      Following a discussion @ Moodle Dev Chat:


      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):


      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.


              Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.