0

About the duplicate vote: This question is about the downsides of this approach, not about how to make this work; You can see it in this way: that question asks how to do something and the answer says something useful, however, I ask about the downsides of the answer.

TL;DR: will I encounter any technical difficulty by using this wrapper class later on? Why I haven't seen anything like this before?

I've been experimenting learning techniques for profiling recently and I found that creating the PDO instance is a place that could be optimized (~5ms). I don't need to use it in every call, however I'm creating it in every call from my code's structure. So, I just made this small class up:

<?php
namespace Library;

// Wrapper for \PDO. It only creates the rather expensive instance when needed.
// Use it exactly as you'd use the normal PDO object, except for the creation.
// In that case simply do "new \Library\PDO($args);" with the normal args
class PDO
  {
  // The actual instance of PDO
  private $db;

  public function __construct() {
    $this->args = func_get_args();
    }

  public function __call($method, $args)
    {
    if (empty($this->db))
      {
      $Ref = new \ReflectionClass('\PDO');
      $this->db = $Ref->newInstanceArgs($this->args);
      }

    return call_user_func_array(array($this->db, $method), $args);
    }
  }

To call it you only need to modify this line:

$DB = new \Library\PDO(/* normal arguments */);

And the type-hinting if you are using it to (\Library\PDO $DB).

It works flawlessly. It's also blazing fast (~0.2ms) when not using the PDO object and only introduces those ~0.2ms delay when using it (completely negligible). Now, I'm still learning about proper OOP, namespaces and general code structure, so I think I'm not qualified enough as to answer my own question yet:

Will I encounter any technical difficulty by using this wrapper class later on? Why I haven't seen anything like this before? therefore, why is this not more common or even default PDO behaviour?

Note: I intend to extend it furthermore by adding few more methods.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • 1
    Looks like a basic singleton pattern to me, but using reflection to instantiate in the way you'd do with a factory pattern – Mark Baker Jan 11 '14 at 23:31
  • @MarkBaker, it's not exactly a factory pattern because there's only one class being created. – Francisco Presencia Jan 11 '14 at 23:32
  • @MarkBaker lol that radical change on the comment was funny. It's also not singleton, since I'm passing it around (not global) and I allow for several instances of `\Library\PDO` to exist (not **single** ton). – Francisco Presencia Jan 11 '14 at 23:36
  • possible duplicate of [How to properly set up a PDO connection](http://stackoverflow.com/questions/11369360/how-to-properly-set-up-a-pdo-connection) – PeeHaa Jan 11 '14 at 23:37
  • OK, I'll rephrase, a multiton – Mark Baker Jan 11 '14 at 23:39
  • @PeeHaa can you please provide some insight of how this question is related to that one? Neither my goal nor the means are similar to that link. – Francisco Presencia Jan 11 '14 at 23:41
  • It does the exact same thing as you are trying to do only without reflection. – PeeHaa Jan 11 '14 at 23:42
  • But please explain what the actual optimisations are for creating the PDO object.... because I can't particularly see where it's any faster than simply doing new \PDO – Mark Baker Jan 11 '14 at 23:42
  • @MarkBaker, in every request that the \PDO object is not used. Basically I'm lazy loading the object without having to pass the initialization parameters all around; in another light, I'm combining them in a single entity that is compatible with the *traditional* PDO object. So, gaining in speed in some cases and not losing in complexity. – Francisco Presencia Jan 11 '14 at 23:47
  • BTW I would say 5ms is also pretty negligible when looking at an entire request / execution but hey... – PeeHaa Jan 11 '14 at 23:52
  • @PeeHaa, I can see how similar they are now. There's a difference though, that when creating the PDO object the arguments are passed in-situ, however this is small. A bigger difference would be about the nature of the question, I'm asking about downsides/technical problems with this approach, however now I have somewhere to start looking at. Thank you (: – Francisco Presencia Jan 11 '14 at 23:54

1 Answers1

1

To answer your question: There is nothing wrong with this. This is a common optimization pattern, known as lazy loading, as you also mention in the comments.

While it could be implemented in different ways than what you have done here, your approach looks perfectly fine to me.

Frederik Wordenskjold
  • 10,031
  • 6
  • 38
  • 57
  • I said lazy load thinking that it was normally done just by not doing something yet, not by giving the *problem* to another class in the way I did. I actually didn't realize that it was lazy load until commenting. After reading [that wikipedia's article section](https://en.wikipedia.org/wiki/Lazy_loading#Ghost) and [this SO answer](http://stackoverflow.com/a/9911787), what I did is actually called a Ghost. – Francisco Presencia Jan 12 '14 at 00:03
  • Haven't heard about the "ghost" before. But yes, that is indeed a ghost you're making :) – Frederik Wordenskjold Jan 12 '14 at 00:09