Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,382 members, 7,808,358 topics. Date: Thursday, 25 April 2024 at 10:49 AM

Need Help PHP OOP - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Need Help PHP OOP (537 Views)

OOP In Php For Beginners / Learn PHP(OOP) And Mysql With PDO By Building A P2P Donation Website / What Is The Purpose Of Frameworks And OOP In PHP? (2) (3) (4)

(1) (Reply) (Go Down)

Need Help PHP OOP by Ayemileto(m): 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(m): 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(m): 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(m): 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

1 Like

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

1 Like

Re: Need Help PHP OOP by Ayemileto(m): 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)

New To Blogging? This One Secret Can Make You Start Earning Faster / Https://smartwebo.com/8-best-email-marketing-software/ / Advertising Secrets

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.