, , , , , ,

A class is a blueprint. A plan of doing something. Suppose we want to keep reviews through a class. Objects contain data and methods to send and receive messages.
It decides how a ‘review’ object will behave in future. Every object may not behave in the same fashion because one review may remain incomplete. You’ve to think every possibility before planning a class. It’s a good practice to have your pen and notebook ready so that you can write it down completely. What you’re going to do will be your flow chart. And the code you write inside your class will be your algorithm.
Now we have a ‘Review’ class.

We’ve decided to keep three properties. Category, title and content. Whenever we create an object or instance of this class, these three properties will be passed through the constructor method. Next we wanted to add one more property. We name this property ‘complete’. By default we set this property value ‘FALSE’.
Only when the ‘review’ object hit the ‘complete’ method, this property will change to ‘TRUE’.
Let us view the code first.

category = $category;
$this->title = $title;
$this->content = $content;
echo $category . "=" . $title . "=" . $content;

public function completed() {
$this->complete = TRUE;
echo '....Review Completed';

public function notCompleted() {
echo $this->complete . '....Review Not Completed';

$review1 = new Review('Cinema', 'The Enemy of the State',
'A very good movie. It is full of action and drama.');
echo '
echo '
$review2 = new Review('Cinema', 'The Edge');
echo '

In the code section it’s clear that we set the properties first. Next we set two methods. We have also created two instances of the class ‘Review’.
The first instance calls the ‘complete’ method. But the second instance or review object is not complete.
Let us have a more detail introspection of this class ‘Review’. Whenever an instance or object is created, the constructor method is called. For that reason we decided to pass the arguments through it. Those arguments or parameters may contain the value. We wanted to write simple reviews, where a category, a title will be kept up with the body content. We’d also like to mention whether our review is completed or not completed.
The logic is simple and the algorithm is not very complex. Look at the separation of the code. The first object does not get affected when the second object remains incomplete.
The advantage and strength of Object Oriented Programming lie here. You can keep everything separated and loosely coupled. Imagine the functioning of the car parts. If your horn does not work it’ll not affect any other part. If you want to stop the car, apply the brake and you can stop it. Our code is also telling about that separation.
Now we’ll see the output of our code first.

//the first object
Cinema=The Enemy of the State=A very good movie. It is full of action and drama.....Review Completed
object(Review)#1 (4) { ["category"]=> string(6) "Cinema" ["title"]=> string(22) "The Enemy of the State" ["content"]=> string(50) "A very good movie. It is full of action and drama." ["complete"]=> bool(true) }
//the second object
Cinema=The Edge=Content to be filled up.....Review Not Completed
object(Review)#2 (4) { ["category"]=> string(6) "Cinema" ["title"]=> string(8) "The Edge" ["content"]=> string(24) "Content to be filled up." ["complete"]=> bool(false) }

From the output it’s clear that what we wanted at the very beginning finally ends up successfully.

For further reading