Jump to content

Please explain OOP - Driving me nuts


bobzob

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.