Jump to content

Example of the Usefullnes of O.O.P.


947740

Recommended Posts

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.

Link to comment
Share on other sites

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(...);

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.