Creating a MySQLi wrapper with the Singleton Pattern (Part 1/2)

preview_player
Показать описание

Official site

Twitter
Рекомендации по теме
Комментарии
Автор

Thank you very much kind sir, for teaching others what you know

NitzanB
Автор

Great tutorial. I've seen few singleton examples today and they all were very confusing to me, but after seeing this video it 'clicked' in my mind almost instantly.

adiachnko
Автор

Constructor need to be private in Singleton!

danijelmacakanja
Автор

If $instance was protected, you would only be able to access it from inside a class that extends the DB class. Check the manual.

ryansmith
Автор

Hi Alex, does this method also applies to PDO as well ?

welovephp
Автор

Cool, singleton seems to be quite a controversial pattern to use, a lot of people suggest never using it at all, what's your opinion of it? Oh and more design pattern videos would be great! :)

supernickynick
Автор

Alex, so I looked at this in another of your series and found it very useful however I ran into problems when I wanted to query the DB a second time while looping through the results of the first query. A simple example might be query of car manufacturers and then for each manufacturer query all car models to then list the car models under each manufacturer.

example
$mDB = new DB(); // manufacturers object
$manus = $mDB->query(SELECT * FROM manufacturers);
$cDB = new DB(); // cars

foreach($manus->results() as $manu){
$cars = $cDB->query(SELECT * FROM cars WHERE manufacturer, array($manu->name));
echo $manu->name;
foreach($carss->results() as $car){
echo $car->name;
}
}
I modified this class to allow me to call it multiple times, not considering the fact that I am making at least two connections and possibly three. Watching this second video (and I don't quite know what brought me to do so) I realise I missed the point of this singleton method.

So with that in mind what is the best approach here: would it be to store the results of the queries in arrays (while using the single connection) one would contain my list of parent objects (manufacturers) and then the second array (car models) would be re populated with the results of the second query for each loop of the initial array.

example (syntax maybe a bit off but hoping it gets the principle across).
$DB = getInstance::DB();
$manus = array();
$carss = array();
$manus[] = $DB->query(SELECT * FROM manufacturers)->results();

foreach($manus as $manu){
$cars[] = $DB->query(SELECT * FROM cars WHERE manufacturer, array($manu->name))->results();
echo $manu->name;
foreach($cars as $car){
echo $car->name;
}
}

CraigDavison
Автор

And shouldn't the constructor be private as well? Because if it's public, you can instantiate as often as you want and misuse the single-pattern.

amsfuy
Автор

Where can I jump from the normal php script to this current "stylish" -> kind of php? I find this very short and seethrough, but I used to do the normal php programming (arrays, strings, etc). Thanks in advance! :)

dgloria
Автор

Is there any code repository for this lesson?

lamelamelazza
Автор

thanks, so where we can clone the DB class ?

iMezied
Автор

i can't get t to work can't find the code in developphp form

JoseHNCGroupB
Автор

Beatrice you use this-> when you are working in a class

Knards
Автор

mysqli doesn't work for me. I have to use mysqli_connect. It works just fine, I only wonder why?

crilleAaa
Автор

hey, gr8 tutorial but i get this F*%#k error msg and cant fix it :)
do you no how to fix this? 

Fatal error: Class 'mysqli_connect' not found in C:\wamp\www\model\db.php

lowkar
welcome to shbcf.ru