colinmollenhour / mongodb-php-odm

A simple but powerful set of wrappers for using MongoDb in PHP (also a Kohana 3 module)
BSD 3-Clause "New" or "Revised" License
208 stars 50 forks source link

About Mongo_Document::factory #53

Closed sergeyklay closed 11 years ago

sergeyklay commented 11 years ago

Hi!

I'm not sure, maybe I do not understand something or something has overlooked ... Can somebody explain to me what the role self::$models here:

  /**
   * Instantiate an object conforming to Mongo_Document conventions.
   * The document is not loaded until load() is called.
   *
   * @param   string  $name
   * @param   mixed   $load
   * @return  Mongo_Document
   */
  public static function factory($name, $load = NULL)
  {
    if (isset(self::$models[$name]))
    {
      $class = self::$models[$name];
    }
    else if (strpos($name, '\\') !== false)
    {
      $class = $name;
    }
    else
    {
      $class = 'Model_' . implode('_', array_map('ucfirst', explode('_', $name)));
    }
    return new $class($load);
  }

I'm not found any place where this may be reading. It is evident that it is only written. Excuse me if I say nonsense, I'm just a little confused here...

colinmollenhour commented 11 years ago

It is public so I suppose the user could specify their own factory name to class mapping. E.g.:

Mongo_Document::$models['foo'] = 'Model_My_Foo';
sergeyklay commented 11 years ago

@colinmollenhour thank you! Hmmm, in my opinion it is dubious (or at least excessive) benefit, as compared with existing ways

colinmollenhour commented 11 years ago

Yes, using a public static variable is indeed a bit dubious.. A static setClassNames method or similar would have been less dubious.. But, I think it can be a very useful feature if you want to use the library outside of Kohana 3 or if Kohana ever changes their class name scheme again.

sergeyklay commented 11 years ago

good