Classes & Objects


computer apg

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:





class

Dwarfs



objects

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.



 keywords 


You cannot use any of the following words as constants, class names, function or method names.

Using them as variable names is generally OK, but could lead to confusion.

As of PHP 7.0.0 these KEYWORDS are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.


KEYWORDS
__halt_compiler() abstract and array() as
break callable
(as of PHP 5.4)
case catch class
clone const continue declare default
die() do echo else elseif
empty() enddeclare endfor endforeach endif
endswitch endwhile eval() exit() extends
final finally
(as of PHP 5.5)
for foreach function
global goto
(as of PHP 5.3)
if implements include
include_once instanceof insteadof
(as of PHP 5.4)
interface isset()
list() namespace
(as of PHP 5.3)
new or print
private protected public require require_once
return static switch throw trait
(as of PHP 5.4)
try unset() use var while
xor yield
(as of PHP 5.5)
yield from
(as of PHP 7.0)
ed48