tags:

views:

207

answers:

6

Possible Duplicate:
What are the benefits of OO programming? Will it help me write better code?
OO PHP Explanation for a braindead n00b

Just started learning/playing with creating classes in PHP and I'm wondering what pain do they solve? It seems like I can get the same job done with just a collection of functions that I include into the file. So my question is: Why should I use classes?

+1  A: 

Basically, classes allow you to put your data with the code - i.e. organization.

Also, classes allow your "followers" to customize your classes without rewriting your code, but rather creating new inherited classes.

Every class-based code might be rewritten with functions, but it would be much harder to understand.

BarsMonster
A: 

It a way to make your code more granular, with proper data hiding, separation of concerns and some other best practices.

IMO using only functions in your code sooner or later leads to spaghetti-code that is hard to maintain and extend. It's harder to fix bugs, its harder to implement new features, because often there are lots of code replication.

Also you can't use polymorphism in your code design, so you can't work with abstractions.

anthares
A: 

the classes/object is the way of implementation object-oriented application design. it covered detailed in numerous OOAD/OOP books.

zerkms
+1  A: 

The Three Pillars of Object Oriented Programming. Learn them well:

http://codeidol.com/csharp/learncsharp2/Object-Oriented-Programming/The-Three-Pillars-of-Object-Oriented-Programming/

Encapsulation

The first pillar of object-oriented programming is encapsulation. The idea behind encapsulation is that you want to keep each type or class discreet and self-contained, so that you can change the implementation of one class without affecting any other class.

Specialization

The second pillar of object-oriented programming , specialization , is implemented through inheritance ; specifically by declaring that a new class derives from an existing class. The specialized class inherits the characteristics of the more general class. The specialized class is called a derived class, while the more general class is known as a base class.

Rather than cutting and pasting code from one type to another, the derived type inherits the shared fields and methods. If you change how a shared ability is implemented in the base class, you do not have to update code in every derived type; they inherit the changes.

Polymorphism

Polymorphism allows values of different data types to be handled using a uniform interface. The primary usage of polymorphism is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time

See also:

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

http://en.wikipedia.org/wiki/Type_polymorphism

Wayne Hartman
+2  A: 

It's a way to view your code in a more intuitive, real-world way. (You package the data and all possible operations on that data together.) It also encourages encapsulation, abstraction, data hiding... What you're really looking for is the advantages of OOP.

froadie
A: 

Generally, its so that you can customize the behavior of that set of functions. Typically you have a bunch of functions that work in concert.

People who use these functions may want to only modify one of them for some special case. Or maybe you provide a class that forces the functions to interact in a certain why, but you can't define what they'll actually do.

A trite example: imagine if you had some library to check that some things didn't overlap.

class Comparator:
  def Greater(self, left, right): pass
  def Less(self, left, right): pass

def EnforceNoOverlap(self, comparator, left, right)
  assert comparator.Greater(left, right) != comparator.Lesser(left, right)
Richard Levasseur