0

I don't know if I can ask about CMSMadeSimple here or not but here goes. I've seen other CMS and framework questions.

I am studying the code of CMSMS so that I can learn about making my own CMS. I think this is really a php and design question but here:

http://phpxref.com/xref/cmsmadesimple/nav.html?_functions/index.html

under CMSModule which is the class all have to inherit from they have this code:

class CMSModule
 479  {
 480      /**
 481       * ------------------------------------------------------------------
 482       * Initialization Functions and parameters
 483       * ------------------------------------------------------------------
 484       */
 485      var $cms;
 486      var $curlang;
 487      var $langhash;
 488      var $params;
.....
 509    function CMSModule()
 510      {
 511          global $gCms;
 512          $this->cms =& $gCms;
 513          $this->config =& $gCms->GetConfig();
 514  

What is that last part saying? I don't understand it. Especially when lower in the class it has:

753      /**
 754       * Returns the cms->config object as a reference
 755       */
 756      function & GetConfig()
 757      {
 758          global $gCms;
 759          $config = &$gCms->GetConfig();
 760          return $config;
 761      }
 762  
 763      /**
 764       * Returns the cms->db object as a reference
 765       */
 766      function & GetDb()
 767      {
 768          global $gCms;
 769          $db = &$gCms->GetDb();
 770          return $db;
 771      }
 772  

These look like they almost do nothing or that they keep calling themself forever....with no real db stuff to boot.

I am hoping to understand the design here with all the calls by reference.

Thank you.

johnny
  • 19,272
  • 52
  • 157
  • 259

1 Answers1

2

To start with the CMSModule class looks like it is using containment to semi-derive from whatever the type of object $gCms is. It does NOT look like they're the same type of object, or there would indeed be infinite recursion issues. It almost looks like this PHP code was developed before v5 and good inheritance? There is probably a CMS class, but you're reading the CMSModule class, if that makes more sense.

To elaborate, the function & GetConfig is just returning a reference to what's returned by the ->GetConfig() call on the global object $gCms. Apparently, this is to implement a singleton pattern, so that there's one main configuration that's used and always returned. A reference to that config and db is also retained (overwritten) into the CMSModule object. This isn't always a great idea, but does make sense in this case, as it is used commonly for this type of an application, and in php in general (global db objects, etc).

Community
  • 1
  • 1
zanlok
  • 1,608
  • 1
  • 16
  • 29