Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use classes rather than just a collection of functions? [duplicate]

Tags:

oop

php

class

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?

like image 417
JoeCortopassi Avatar asked Feb 18 '10 00:02

JoeCortopassi


1 Answers

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

like image 82
Wayne Hartman Avatar answered Oct 02 '22 12:10

Wayne Hartman