Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the class is instantiated once, then you're going to be just fine. In this case, you open one connection only.</p> <p>If you instantiate the class several times, or use it as a static class, then you're potentially going to create a connection each time, which is not ideal.</p> <p>If you keep the classes all active (i.e. you never delete reference to a class once you create it, from memory (I've never tested it) the internals of PHP should actually sort this out for you, and you'll still only have the one connection to the database. But if you lost a handle to the class you created, then the PDO will be destroyed and you start again. If you are using as a static class (i.e. you call it with <code>class:function()</code>), then you're on the wrong path.</p> <hr> <p>There are two common solutions, and you'll find arguements for and against both of them. </p> <p>1) Use a global for storing your DB connection. You create the $handle = PDO and store $handle as a global variable. Easy to pass around. Easy to overwrite - I'm not going to debate here.</p> <p>2) Create a "static" class (commonly called a Singleton) that you recall. The basic structure would be</p> <pre><code>private static $ThisObj; private static $handle; public static function getInstance() { if( !(self::$ThisObj instanceof SoapDB) ) { self::$ThisObj = new SoapDB(); try { $this-&gt;handle = new PDO_Handler('mysql:dbname=' . DB_NAME . ';host=' . DB_HOST, DB_USER, DB_PASS); $this-&gt;handle-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { // Error Here } } return self::$ThisObj; } </code></pre> <p>Using this, you would normally create a separate DB class as a singleton. Using Singleton vs Global: your call (after research).</p> <p>3) The third answer is if you are only ever calling your class above as a static (i.e. you call <code>class::function()</code>) then you can just make the $handle as a static and actually your problems should be wonderously solved. Essentially you're creating a Singleton, with the database as one of the properties.</p> <p>Sorry that there's no wrong or right answer. </p> <hr> <p>Edit based on comment "What's a static class":</p> <p>Strictly speaking, I should say "class with static properties". To explain, normally you create a class with the syntax:</p> <pre><code>$myClass = new class(); $methodResult = $myClass-&gt;method(); </code></pre> <p>and then do lots of nifty little things with <code>$myClass</code> by calling functions. You can then drop reference to the class, lose the properties (variables) and will reinitilaise later on.</p> <p>A static class (for the purposes of this answer) is one where you have static properties. Normally (not absolutely) they are called by going.</p> <pre><code>$methodResult = class::method(); </code></pre> <p>You don't hold on to the class - it gets initiated, used and dropped. However, if you store properties (<code>var</code>s) as static then each time you use that class, those properties will still exist in the state they last were. So <code>method</code> can set <code>$this-&gt;MyVar = 'something'</code> and next time, $this->MyVar will still be something.</p> <p>Very useful for compartmentalising your code, stops functions overwriting others and can make unit testing easier. </p> <p>(Note - I'm generalising to make it simpler. But it should give you an idea of the processes.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload