Real World OOP examples in PHP ?
Moderator: General Moderators
Real World OOP examples in PHP ?
I have been coding in PHP4 for the last one year. Have no experience with OOP or other High Level languages though.
lately trying to learn OOPs in PHP ..
There are plenty of tutorials out there but each one talks in terms of absurd examples which i dont see of much use.
I am not able to relate how i could use them in solving real world issues .. say for managing logins, membership details etc etc..
Have you come across such tutorials (preferably free ones) that can give me some insights into:
1) WHY OOP ?
and
2) How OOP ??
Thanks..
lately trying to learn OOPs in PHP ..
There are plenty of tutorials out there but each one talks in terms of absurd examples which i dont see of much use.
I am not able to relate how i could use them in solving real world issues .. say for managing logins, membership details etc etc..
Have you come across such tutorials (preferably free ones) that can give me some insights into:
1) WHY OOP ?
and
2) How OOP ??
Thanks..
Re: Real World OOP examples in PHP ?
That was the hardest thing for me, as I learn best from taking an example and applying/tweaking it to what I need. back when I took classes on OOP years ago, they aslo did the simplistic examples, which made it really hard for me to see real good use of them. After all, not every thing needs to be a class. It can be tough figuring out what best works as a class and then how to properly set them up. I admit, I still shy away from them. I still have trouble understanding the idea that for any simple call to something for a web app, that it needs to load up every class and have it available (granted the framework I used it on, may not have been best way)
I do come from old school programming (64k for EVERYTHING back on my old Commodore computer), and am picky about only loading what is needed on page requests.
I would like to find as well a good real world example that properly does best practices on using OOP for PHP. Until then, I do things "old school"
-Greg
I do come from old school programming (64k for EVERYTHING back on my old Commodore computer), and am picky about only loading what is needed on page requests.
I would like to find as well a good real world example that properly does best practices on using OOP for PHP. Until then, I do things "old school"
-Greg
Re: Real World OOP examples in PHP ?
thanks twinedev.
heartening to know, am not the only one struggling
Any one to rescue ?
heartening to know, am not the only one struggling
Any one to rescue ?
Re: Real World OOP examples in PHP ?
Download some modern php apps like modx cms or vanilla forum and take a look at some of the code those developers came up with.
Re: Real World OOP examples in PHP ?
Thanks Doug G, Will try these.
But anything simpler than CMS or Forum, considering that I am still a beginner
But anything simpler than CMS or Forum, considering that I am still a beginner
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Real World OOP examples in PHP ?
This may be difficult for you because you might not have problems where OO is an large advantage to you. You familiarity with the trade-offs in your current code may outweigh the difficulty of learning OO. The point of OO is simply that is solves various problems that come up in software development.Live24x7 wrote:There are plenty of tutorials out there but each one talks in terms of absurd examples which i dont see of much use.
I am not able to relate how i could use them in solving real world issues .. say for managing logins, membership details etc etc..
(#10850)
Re: Real World OOP examples in PHP ?
O.K taking one example, from links of smith001
Now why do i need to instantiate the entire class, if i only need to connect to the database ?
Isn't that inefficient use of resources ?
And what was wrong with plain old functions ?
Code: Select all
class Database
{
public function connect() { }
public function disconnect() { }
public function select() { }
public function insert() { }
public function delete() { }
public function update() { }
}
Isn't that inefficient use of resources ?
And what was wrong with plain old functions ?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Real World OOP examples in PHP ?
You say "entire" like is is a huge amount of work and code. The class above only adds a few additional lines beyond the functions. But what value might you get from putting shared variables related to a database connection in a common namespace?Live24x7 wrote:Now why do i need to instantiate the entire class, if i only need to connect to the database ?
No. Compared to the time it takes to do the database connection, the difference between using functions and classes is insignificant.Live24x7 wrote:Isn't that inefficient use of resources ?
When you can answer that question for yourself, then you will know. Otherwise, structured programming is sufficient for you.Live24x7 wrote:And what was wrong with plain old functions ?
(#10850)
Re: Real World OOP examples in PHP ?
Having started programming near the end of the stone age on a TI-99/4a, I'll try to explain my take having recently "converted" myself.Live24x7 wrote:Now why do i need to instantiate the entire class, if i only need to connect to the database ?
Why? Because next week you might need to insert, update and delete stuff too. And then perhaps next month you need to connect to a remote database as well. Now you can use two objects, one for the local database and one for the remote database. Need to synchronize the databases? You can create a new object that works with database objects to compare and update tables. What if you want to switch from MySQL to Oracle? Can you reuse all your old code that calls msqli_ all the time? Not unless you've written functions to handle all your database calls. But with OOP the code is more flexible because you are calling methods (functions) for everything.
The big advantage is you can reuse all your code and it is kept in a nice neat place that all has to do with databases. It's also easy to update and if you are working with other programmers, they can quickly see what you're doing, reuse, modify or extend your code.
I also find it easier to read OO code, often without having to dig into the classes to see what exactly is going on. This is rarely the case with procedural code where I have to follow the code around from point to point.
OOP is probably over hyped, but there are advantages in how the data and code are grouped together. After all the point of programming is to make code to manipulate data. OOP's structure is based exactly on that concept. Group those things together then write methods specifically for that data.
Also it works especially well for state dependent systems where in procedural code I would have to spend a lot of time designing a polling sort of system with interrupts. GUI's, real-time processes, games, etc.
I can tell you my transition to OOP has been a long love-hate process. But I'm converted now...well for the most part.