CLASSES AND OBJECTS
By: Stefan Mischook
We are just about to start creating/writing our first objects! But first, we need to cover
just a few bits of theory, so that we can be sure your feet will hit the ground running …
when we actually write some code.
PHP objects - some nerd details:
Objects (in object-oriented PHP) are just like the bricks used to build a building:
Objects are the building blocks of object-oriented PHP projects.
So what are objects?
In a nutshell: an object is just PHP code wrapped up in package. Objects sort of look like
functions (in code,) but are a lot more powerful. More on that later.
… PHP scripts built with object oriented PHP, are just a bunch of objects that work
together to make the whole system. An analogy could be a restaurant (the main script,)
that has a bunch of employees (objects,) working together at the restaurant, where each
employee (object) takes care his or her job.
Contrast this to classic PHP, where you would have a bunch of functions, variables and
other code floating around willy-nilly. OO PHP is about creating modular code that is
contained in virtual containers called: objects.
… I'm repeating myself a little, but I want to drive the point home.
Objects: Functions on steroids:
One way I describe objects (to help people understanding what they are) is to call them
'functions on steroids'.
In the strict nerd sense, this is not exactly true. But when you look at actual PHP code,
PHP objects kind of look like functions because PHP objects will have many of the same
sort of things in them … things like:
That said, PHP objects have all kinds of extra capability that functions do not have. More
on that later.
WHAT IS A 'CLASS'?
Before you can build an object in PHP, you need to create a blueprint for the object … an
outline, a template.
A template (for an object) is called a 'class'.
More on classes:
Classes are the blueprints fo