Note that there are some explanatory texts on larger screens.

plurals
  1. POHave I missed the point of object oriented programming?
    primarykey
    data
    text
    <p>Recently, I took it upon myself to try and learn OO programming. It has been about 3 months since I started, but I think I might be <em>missing the point</em> because I seem to prefer static methods (which seem 'easier' to me).</p> <p>Example</p> <p>Here is what a typical DB query looks like in my code.</p> <pre><code>$bindings = array(':name'=&gt;$articleName); Db::query('SELECT id, name, title, image, content FROM ' . CONFIG_MYSQL_TABLE_PREFIX . 'articles WHERE name = :name LIMIT 1', $bindings); </code></pre> <p>And here is how I resize/crop/cache images</p> <pre><code>$image = Img::thumbnail($imagePath, 200); $imgHtml = '&lt;img alt="' . $this-&gt;getTitle() . '" src="' . '' . $image['src'] . '" width="' . $image['width'] . '" height="' . $image['height'] . '" /&gt;'; </code></pre> <p>Both of the static methods utilise a singleton pattern.. the first one creates one PDO object and the second one creates one ImageResize class I found on Google code.</p> <p>Should these be 2 objects if I really wanted to call it object oriented programming? i.e.</p> <pre><code>$db = new Db(); $image = new Image($src, $width, $height); </code></pre> <p>For everytime I use them? I've read singletons are also a bad idea unless they're being used for logging to a file. But isn't a singleton good for one DB connection being opened when needed and closed only after it's been used and finished with?</p> <p>My question is, am I still stuck in the procedural mindset, and if so, is what I'm doing considered bad practise? How can I immerse myself in the correct OO thought patterns?</p> <h2>Update</h2> <p>Thanks for the answers. I do find the original methods I'm doing are <em>easier</em> as I have to type less code and let the static methods worry about little implementation things.</p> <p>I will look into another language to get a solid grasp of OO, which language though will be another question itself.</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.
 

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