Classes & Objects
Classes & Objects are the two main aspects of
Object-Oriented Programming, OOP.
Let's assume the following illustration as an example of differences between classes and objects:
classDwarfsobjects
Doc
is very wise and likes to think carefully about problems.
He tries to be the leader but he's not always very good at it because he gets very nervous.
Grumpy
No matter what anyone says, Grumpy is always complaining.
He never agrees with anyone!
But, if any of his friends are in trouble, he's always first to the rescue.
Happy
is always cheerful and happy about everything.
He thinks everything is funny and just can't help making jokes.
Sleepy
is very lazy.
He likes to sleep as much as he can when he's not at work in the mine.
He yawns so much because he just can't stop himself.
Bashful
is very, very shy!
He‘s always blushing and twisting his beard into knots.
He loves listening to stories and is always fluttering his eyelashes at Snow White.
Sneezy
doesn't sneeze all the time, just at the wrong time like when he doesn't want anyone to know where he is!
Dopey
isn't really dopey.
He just likes having fun and playing tricks.
He never minds if he looks silly while he‘s playing his games!
Dopey always tries to steal kisses from Snow White before he goes to work.
As an analogy the class Dwarfs is made up of seven objects, (Doc, Grumpy, Happy, Sleepy, Bashful, Sneezy and Doppey), where each one presents its own characteristics and behaviors.
In other words, class is a set of distinct objects, but with the same characteristics and behaviors.
A class is an abstraction of existing entities in the real world.
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
The class name can be any valid label, provided it is not a PHP reserved word.
These words have special meaning in PHP.