₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,151 members, 8,420,570 topics. Date: Friday, 05 June 2026 at 02:33 AM

Toggle theme

Need Help PHP OOP - Webmasters - Nairaland

Nairaland ForumScience/TechnologyWebmastersNeed Help PHP OOP (709 Views)

1 Reply (Go Down)

Need Help PHP OOP by Ayemileto(op): 5:06pm On May 18, 2018
Hi guys,

I just dived into Object-Oriented programming style for PHP from procedural.

The issue i have now is, the functions within the Classes don't get to see the database connection Variable and i have to include it into each functions.

What other way can i do this without repeating the connection code for each function?

Also apart from the classes, the code i have been writing look like ordinary procedural form of writing and i've been wondering if i'm doing the right thing. is it normal for the code written in OOP style to look much like Procedural?


i have downloaded different PHP source code from free source codes site in order to check if i'm doing the right thing. but all the codes i have downloaded so far are written in procedural, probably by learners like me.
if you have a fully functional source code(like a blog) written in OOP that you can give out, i won't mind getting it.

Thanks.
Re: Need Help PHP OOP by Ayemileto(op): 5:13pm On May 18, 2018
For the first question, i discovered by passing the connection variable as an argument to the functions, i can use them without creating a new instance for the function.

Is this good programming practice?
Re: Need Help PHP OOP by javascriptDev: 10:57pm On May 18, 2018
Best approach: Use a singleton class, use static functions, declare your class constructors as private: if you don't understand the reason you should do those then you are better of using globals (not the best approach)


global $conn = mysqli_connect ("localhost", "root", "", "database"wink;
global $conn;

function someFunction () {
global $conn;
$result = mysqli_query ($conn, "SELECT * FROM examples)
}





or something as basic as:






class Db
{
private static $conn;

public static function getObject()
{
if (!self::$conn)
self::$conn = new mysqli("localhost", "root", "", "database"wink;

return self::$conn;
}
}

function someFunction () {
$result = mysqli_query (Database::$conn, "SELECT * FROM examples)
}
Re: Need Help PHP OOP by javascriptDev: 11:06pm On May 18, 2018
Ayemileto:
For the first question, i discovered by passing the connection variable as an argument to the functions, i can use them without creating a new instance for the function.

Is this good programming practice?
Creating new instance for $conn each time you need it Is a bad idea, no scalabilty, u might even end up creating more connections than your server can handle
Re: Need Help PHP OOP by Ayemileto(op): 7:34am On May 19, 2018
javascriptDev:
Best approach: Use a singleton class, use static functions, declare your class constructors as private: if you don't understand the reason you should do those then you are better of using globals (not the best approach)


global $conn = mysqli_connect ("localhost", "root", "", "database"wink;
global $conn;

function someFunction () {
global $conn;
$result = mysqli_query ($conn, "SELECT * FROM examples)
}





or something as basic as:






class Db
{
private static $conn;

public static function getObject()
{
if (!self::$conn)
self::$conn = new mysqli("localhost", "root", "", "database"wink;

return self::$conn;
}
}

function someFunction () {
$result = mysqli_query (Database::$conn, "SELECT * FROM examples)
}
Thanks. I think the second one should be called with Db::$conn
Re: Need Help PHP OOP by Ayemileto(op): 7:35am On May 19, 2018
javascriptDev:
Creating new instance for $conn each time you need it Is a bad idea, no scalabilty, u might even end up creating more connections than your server can handle
Thanks. What about the code looking much like procedural? Is it normal?
Re: Need Help PHP OOP by javascriptDev: 7:33pm On May 20, 2018
Ayemileto:
Thanks. What about the code looking much like procedural? Is it normal?
perfectly normal if you are just starting out with oop php. But try more frequently to group those ”procedures” into functions and wrap them in classes. That way you use mostly objects in your app
Re: Need Help PHP OOP by javascriptDev: 7:33pm On May 20, 2018
Ayemileto:
Thanks. I think the second one should be called with Db::$conn
yes, my bad
Re: Need Help PHP OOP by Ayemileto(op): 12:06am On May 21, 2018
javascriptDev:
perfectly normal if you are just starting out with oop php. But try more frequently to group those ”procedures” into functions and wrap them in classes. That way you use mostly objects in your app
Thanks bro.
1 Reply

OOP In Php For BeginnersLearn PHP(OOP) And Mysql With PDO By Building A P2P Donation WebsiteWhat Is The Purpose Of Frameworks And OOP In PHP?234

Pokemon Quest unlimited PM tickets hack download Android iOSMake Money Doing B-t-c MiningI Want Anyone Who Can Setup An Amazon Associate Account For Me