tags:

views:

141

answers:

6

I dont know much about classes, but have a reasonable knowledge of PHP/MySQL. But why should I learn classes? I know they are important but what benefits can I see using them that I cant with?

+2  A: 

Actually, I've found that it is often simpler to use function-based programming in php than object oriented. There are a lot of ways in which just using function libraries and trying to keep your code simple and direct make your php scripts more maintainable, especially if you minimize state to increase reproducibility.

Objects & classes are one tool that you should get to know so that you can choose between the different options, but certainly not the only choice, now that php 5.3 has first class functions, moving more in the direction of true functional programming is another tool that you could get to know.

Php's background is very function-based, a huge portion of the native language provided is functions and sometimes the square peg of objects doesn't fit in php's round hole.

This certainly wouldn't apply to Java, but php's background is very rooted in functions.

Edit:

Let's be clear, to be effective at php you will have to have a good grasp of objects, because you are going to encounter it frequently, and if you work for other people in php, you'll probably have to write OO yourself on your employer's behalf. So get to know it well. But don't stop at OO and consider it the be-all-end-all solution. In fact, when it comes to dealing with other people's bad php code, I have found solid function-based programming to often be a simpler tool for refactoring and cleaning up bad code.

I would say there are a few ways to write php code:

  1. Bad procedural code (little reusability, probably uses functions, but not well, probably uses objects, but not well).
  2. Good function-based code (maximized reusability, minimized complexities of state, separation of concerns)
  3. Bad object oriented code (large multifaceted objects, complex hierarchies, etc)
  4. Good object oriented code (specific-task objects, clear separation of concerns like MVC)

And eventually, as php 5.3 matures, we'll be able to start throwing in a bit more functional programming into the "good function-based code" category and it will become an even more effective alternative. In the meantime, though, get comfortable with 2 and 4 because you'll need 'em both.

Tchalvak
Why the down-vote?
Jørgen Fogh
Can you be a bit more specific about what makes the use of classes in PHP undesirable? The vast majority of frameworks (including Zend framework) and PEAR libraries are heavily object oriented. They have massive adoption. The PHP code behind some of the largest sites out there -- Facebook, mediawiki, wordpress -- is object oriented. Examples of heavily-adopted modern PHP projects that *don't* use objects are few and far between. I can't even think of one, quite frankly.
Frank Farmer
I have been convinced of the problems of the OO model mainly by Clojure and it's arguments against mutable state and for a more functional approach, e.g. http://clojure.org/state (a good read). PHP is no Clojure, but there do seem to be benefits to be gained in the form of lowered complexity in using less mutable state, which OO is rife with. I often use third-party Objects in php, but at the same time, 90% of the actual language is straight functions, so I have often found in my work that function-based programming in php was simpler than creating a complex object chain.
Tchalvak
+1  A: 

Classes are important when dealing with code reuse, they also provide well organized and cleaner code, in my opinion.

Depending on specific applications/projects you are working on, classes can make sense.

Update:

Also, it might be worth glancing at, and noting that Wikipedia has a section in Class (computer science) called Reasons for using classes which demonstrate a few key points for using classes.

Also, from your previous questions it seems you do a lot of work with PHP and MySQL. To demonstrate how beneficial classes are, you could create a Connection class that handles connecting to your MySQL database, so any changes made to the database you can edit in one single place, (your Connection class) rather than finding all the lines of code when it's called.

Anthony Forloney
+3  A: 

Encapsulation, for one.

Steve Jobs once used a good analogy (it was in an interview). To paraphrase from memory, he said

If I want my clothes cleaned, and give them to a friend, he will return them cleaned. I do not care if he had to get a cab, got a bite to eat or whatever, I just want to give him my clothes and have them returned clean.

Also, I found the interview. Interesting read.

Basically, he is saying that he doesn't care about the implementation details. That's what OO can do. Hide all the stuff inside a class through visibility and so forth. If you want a list of files from a folder, you could do

$files = new FilesInFolder('your/path');

$files->getByExtension('*.jpg');

You don't care if it uses glob() or readdir().

Update

As opposed to a file full of global functions such as functions.php: you can group all of the functions to specific tools. For example in the example above, you could have getFiles() and filterFilesByExtension() but these 2 related functions will be in the global scope, not to mention the second one will require you pass the files as an array back to it.

alex
+1  A: 

One of the main points of object oriented programming (objects and classes) is to make your programs more modular; when you work on one part, you don't have to work on the details of another part of the program. Depending on what kind of code you are writing, this may or may not be important.

A lot of people on a page like this will claim that a) OOP is the only good way to write programs and b) it is desperately needed for any program and/or home-page. I think if you think of what you are writing as a home-page, you probably do not need OOP, unless it's a very large home-page.

OOP is by far the most common programming paradigm, which makes it important to learn, if you want to be a programmer. However, most PHP is not written in an object oriented style and some of it is ok anyway (even though a lot of it is not).

If you think that the PHP that you are writing is hard to manage, learning OOP is probably a good idea, even if you only get the practice. You might even want to try it out in another language like, say, Java or Ruby. It is probably easier to find good books about OOP in those languages.

Jørgen Fogh
A: 

Classes and objects in PHP make one very important aspect of programming much much easier: abstraction.

Like many languages that have objects in addition to scalar types, objects in PHP simple take the aggregated item concept one step further. Things like arrays/lists let you work with a collection of data as one item. Objects make that just a little easier and also let you have code specific to that collection to manipulate that collection.

How about an example? I was involved in an educational intranet application some years ago. Its prime goal was to track academic progress over time and to do that it has to know about student enrolments, class membership and thus timetables. We needed a data point for a particular scheduled class in time. Initially, it was a list of data parameters passed into functions. Year, term, week, day, period. Then it needed to be returned, as well. Now it was an array. Then we needed specialised functions for manipulating it, mainly turning it into a real time and back again. So I made it an object.

Now, as an object, the conversion routines were thoroughly abstracted away. You could ask the object to convert itself and without the calling code caring or knowing, the object could cache expensive calculations. Code to handle tricky things like daylight savings was written and debugged once. The object just made it so much easier to handle the data.

Much of the PHP code was still function oriented. The startup logic on each page called a dozen functions to get everything going. But there were objects, too, for when the data needed it. And it made a lot of the code a lot simpler and more robust.

staticsan
A: 

As always depends what you have to do, the Object Oriented way can become useful for many reasons:

  • If well designed increase the order inside your projects that means much more code re-usability, reduce the written code, less time spent in troubleshooting.
  • Collaboration
    if you have to work with many other peoples it's much more simple to maintain the code
  • Modularity
    it will give you a very good project organization
  • Encapsulation
    By implementing classes you can hide the code complexity inside the objects and keep "only" the logic part in the main code.
Cesar