947740 Posted May 19, 2008 Share Posted May 19, 2008 When I learned PHP through a book, it had one chapter on O.O.P., which was not very informative on the real, possible uses for it. O.O.P. seems like a good thing, as I have seen by reading posts on these forums, but I do not know of any practical uses for it. In fact, I do not know it at all seeing as I did not see any point in learning it. Does anyone have an example of two ways to do something, one with O.O.P. and one without? Hopefully I will understand the real point with an example. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/ Share on other sites More sharing options...
effigy Posted May 19, 2008 Share Posted May 19, 2008 OOP provides a namespace--a package for the code. Let's say I want to share some code with you and one of my functions is called open_file. Well, if you already have an open_file function in your code, you're going to have to start changing things--not only the function name, but the functions that are calling that function. See how messy this gets? What OOP provides is a singular namespace in which all other functions and variables are encompassed. We could both have an open_file, but yours would be... $your_obj = new Your; $your_obj->open_file(...); ...and mine... $my_obj = new Mine; $my_obj->open_file(...); Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-544941 Share on other sites More sharing options...
trq Posted May 19, 2008 Share Posted May 19, 2008 Unforunately, simple exampels do not usually do OOP justice. It comes into its own on larger more complex problems. Id suggest you get another book, specifically on the subject. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-544942 Share on other sites More sharing options...
deadonarrival Posted May 19, 2008 Share Posted May 19, 2008 The best way to think of it is a way of ORGANISING your code. OOP code doesn't produce a different output to procedural code, and it's not necessarily faster (in fact, it can be slower) Its main advantage is the ability to encapsulate. The ability to make everything about one thing, in one place. Everything to do with database access can be done through a database object, everything purporting to sessions through a session object, everything to do with the layout through a view controller. The idea is that one script calls up the methods of various objects, rather than trying to do everything itself. If done properly, it makes your code tidier and easier to use, modify and add to. It's also usually considered more useful for making frameworks or API's, you can more easily set up a user interface to allow other people to build on your code without having a deep understanding of it. They don't need to know your database structure (or even whether you use mysql, postgre or any other database system), or how you generate passwords, just that $db->getRow(table,field,where); gets the field they want, and that $auth->passwordEncrypt(plaintext,salt); produces a password they want. They don't need to know how you encrypt the password, just that it returns a nice hash for them to use. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-544944 Share on other sites More sharing options...
947740 Posted May 19, 2008 Author Share Posted May 19, 2008 I think I get it. I will think about getting another book. I am guessing it is something that can not be fully understood through a forum. That helped clear it up a bit, deadonarrival. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-544947 Share on other sites More sharing options...
deadonarrival Posted May 19, 2008 Share Posted May 19, 2008 Glad to be a little help. I had big problems understanding it too. It's just a case of reading everything about it until someone finally explains it in a way that clicks. Get a book from a shop, rather than a website - it's easier to find a style you like that way Read websites, books, anything you can get hold of. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-544978 Share on other sites More sharing options...
roopurt18 Posted May 19, 2008 Share Posted May 19, 2008 I recently read Head First Design Patterns from O'Reilly publishing. While there are many possible choices for design pattern books out there, I liked this one because it had lots of pretty pictures and was an easy read over-all, rather than being a textbook approach. The examples used are in Java, which is close enough to PHP in syntax that you should be able to follow along to get the gist of things. While the examples aren't the most real-world, they do get the point across. I'd suggest taking a trip down to your local Borders or other gigantic book store, picking up a copy, and reading a chapter or two. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545136 Share on other sites More sharing options...
947740 Posted May 19, 2008 Author Share Posted May 19, 2008 Thanks. I will take that into consideration. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545159 Share on other sites More sharing options...
rhodesa Posted May 19, 2008 Share Posted May 19, 2008 Another example is the separation of responsibility. When you are coding on a team, it's very useful to use OOP, because you can come up with a agreed up interface, and the person using it doesn't need to know what is going on in the background. A quick (and common) example is an address book. We agree upon a common set of methods: getName() getPhone() getEmail() getAddress() This allows me to work on the area of how the information is stored/retrieved, and allows you to work on how the data is used. Maybe I choose to store it in a text file. But half way into the project I decide to move it to a database. I change my code, but you are unaffected by the change. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545168 Share on other sites More sharing options...
roopurt18 Posted May 19, 2008 Share Posted May 19, 2008 @rhodesa, that is not a property of OOP at all. You could just as easily come up with a procedural interface and the same argument would apply: user_getName(); user_getPhone(); user_getCell(); etc. (Not trying to be a Negative Nancy here. I do agree with the usefulness of a common interface, just stating that it's not a direct property of OOP.) Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545290 Share on other sites More sharing options...
deadonarrival Posted May 19, 2008 Share Posted May 19, 2008 Just to agree with roopert - you could get the same seperation with "normal" procedural PHP Just stick the functions in one file, and make it one person's responsibility, and you're done. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545296 Share on other sites More sharing options...
rhodesa Posted May 19, 2008 Share Posted May 19, 2008 my plot to take over the world with OOP has been foiled :-\ Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545298 Share on other sites More sharing options...
deadonarrival Posted May 19, 2008 Share Posted May 19, 2008 $world = world::getInstance(); $myWorld = new World; $world->activateDoomsdayMachine("fire","true",1,50000); unset($world); Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545302 Share on other sites More sharing options...
947740 Posted May 20, 2008 Author Share Posted May 20, 2008 And it has begun... Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545350 Share on other sites More sharing options...
deadimp Posted May 20, 2008 Share Posted May 20, 2008 The PHP Manual on OOP is a great resource. Nice 'n free. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545395 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 The Zend site is one of my favorite spots for learning PHP. Here is some beginner OOP: http://devzone.zend.com/node/view/id/638 Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545414 Share on other sites More sharing options...
947740 Posted May 20, 2008 Author Share Posted May 20, 2008 Thanks for the links guys. Quote Link to comment https://forums.phpfreaks.com/topic/106330-example-of-the-usefullnes-of-oop/#findComment-545644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.