bobzob Posted July 15, 2008 Share Posted July 15, 2008 Hey, I used PHP 4 a few years ago to create a few websites and found it very easy to use and now i've decided to start using PHP again but this time i've gone for PHP5. I went out and purchased a book PHP & MySQL Web Development which covered PHP 5 and MySQL 5 and in the book it also covered OOP. Now when i used PHP 4 in the past i never used functions or classes (i did have a basic understanding of what they were) so this time i decided to try and have ago at OOP but i must say. I really have no idea why or when to use OOP. To me it just seems to make things more complicated, it uses functions inside classes inside objects to do things i could do in other easier ways. It also says in the book im using to make functions for almost everything, a function for registering users on a website and i must ask Whats the point!?!? im only going to register them once why do i have to make a function to handle this! If anyone knows of a book which can break down OOP in PHP5 in the most simplistic way or another website i would greatly appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/ Share on other sites More sharing options...
trq Posted July 15, 2008 Share Posted July 15, 2008 This question has been asked many, many times. The basic gist is that classes (and even functions) are used to encapsulate code. You can create clean, self contained code without messing up the global namespace. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-591080 Share on other sites More sharing options...
PFMaBiSmAd Posted July 15, 2008 Share Posted July 15, 2008 Edit: Well at least one more time. The point of functions and OOP is to be able to write modular code that performs a specific function and does not interfere (share variable scope) with your main program. By doing this you can write and test functions that your main program logic relies on. Once you know a function works, you can forget about the code and variables in it and concentrate on the main program logic to accomplish what is needed for the overall application. It is actually just a side effect that you can call functions more than once in any code or that you can reuse them in other code. While you can create more than one instance of an OOP class without needing to worry about any interference between them or with your main program, most programs only use a single instance of any class. If you are writing an application that needs many different functions to build the whole application - user login class, database functions/class, form validation function/class... by writing (or reusing existing definitions) for each smaller part, you break the problem down into smaller size pieces. Each smaller piece is defined, coded, and tested (or if it already exists, can simply be included and used.) This allows a larger project to be completed in a more organized manor and this allows a team of programmers to work on a single project. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-591099 Share on other sites More sharing options...
gizmola Posted July 19, 2008 Share Posted July 19, 2008 It also says in the book im using to make functions for almost everything, a function for registering users on a website and i must ask Whats the point!?!? im only going to register them once why do i have to make a function to handle this! If anyone knows of a book which can break down OOP in PHP5 in the most simplistic way or another website i would greatly appreciate it. These are fair questions, and hard to answer when you aren't yet bought into the concept of functions. If you consider PHP for a moment, it should be somewhat self evident that PHP is largely comprised of functions that your scripts call to do different things. But you've found that you can create different scripts to do different things, and link them together, and don't need to actually write functions of your own. Did you consider this example however? In your registration script, there is probably a point where you accept the user's password and store it somewhere. Most systems then have a login form that accepts that password and compares it with the one in the database to see if the user should be logged in. That could be a small block of code to perform the lookup, retrieval and comparison. Now consider, that most systems also have a profile page where users can set a new password, which usually (best practices) has a box where the user has to type their old password in order to update the new one. Why do this? Well the main idea is that this protects someone from stepping away for a moment, and having someone sit down and quickly change their password while they're away from the desk. So in that example you have a whole block of code needed to do exactly the same stuff, but now in two totally different contexts: login and change password. You could certainly cut and paste all the code into both scripts, and it would certainly work fine --- until the day you find that you made a mistake in the code and need to update. Now instead of having a function that is called in both places, instead you have to make the same changes twice, and retest twice. Hopefully this gives you an idea of why functions are extremely useful. Once you buy into that, you may be able to begin to buy into the idea of objects, which are really bundles of variables along with the functions created to act upon and manipulate that data. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-594086 Share on other sites More sharing options...
matthewhaworth Posted July 21, 2008 Share Posted July 21, 2008 http://www.geekpedia.com/tutorial242_Object-Oriented-Programming.html Something I wrote a while back. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-595088 Share on other sites More sharing options...
ignace Posted July 23, 2008 Share Posted July 23, 2008 in oop you also get design patterns ..most programs only use a single instance of any class. .. like PFMaBiSmAd said, he was referring to the singleton pattern Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-597378 Share on other sites More sharing options...
natbob Posted July 23, 2008 Share Posted July 23, 2008 I am also having trouble with when to use OOP. I've never really gotten a strait answer. Is most of it just name spaces, because I can't really see where inheritance and metaporphism fit into anything other than the base language and the most complex extensions like gtk. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-597704 Share on other sites More sharing options...
ignace Posted July 23, 2008 Share Posted July 23, 2008 you mean polymorphism Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-597747 Share on other sites More sharing options...
natbob Posted July 23, 2008 Share Posted July 23, 2008 yes I did mean polymorphism but I don't like php's metamorphic options either! Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-597785 Share on other sites More sharing options...
alexobenauer Posted July 25, 2008 Share Posted July 25, 2008 I actually wrote a lengthy post that explores OOP and it's history. Feel free to read it here, let me know if you still don't understand it. http://discotank.com/developerstoolbox/?p=37 Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-599554 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 @alexobenauer: If you have the ability to revise that article, I'd suggest using PHP 5 OOP syntax rather than the deprecated and nearly useless PHP 4 syntax. Also, you should have used $this->attribute instead of $attribute because otherwise there's no point of defining that class variable. Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-600695 Share on other sites More sharing options...
ignace Posted August 7, 2008 Share Posted August 7, 2008 @natbob php's metamorphic options? i thought rocks had metamorphic abilities or are you referring to the dynamic typing? Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-610515 Share on other sites More sharing options...
paparts Posted August 7, 2008 Share Posted August 7, 2008 www.babycobalt.wordpress.com for php tutorial weel explained Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-610523 Share on other sites More sharing options...
jchemie Posted August 8, 2008 Share Posted August 8, 2008 Okay, I have had a similar experience when I began doing OO Programming. Well let me help you clear your Object Oreinted basics first. I have written an article on object oriented programming on my blog. The url of the post is http://loveofphp.com/programming/object-oriented-programming.html. Hope this is an enjoyable ride for you. Please explain OOP - Driving me nuts Thanks Jyot Vakharia Quote Link to comment https://forums.phpfreaks.com/topic/114927-please-explain-oop-driving-me-nuts/#findComment-611787 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.