Object Oriented Programming and PHP 7

Chapter 1. OOP and PHP 7


For absolute beginners, the term OOP or Object Oriented Programming may seem difficult. Some people want to mystify this term very heavily! Why? Because, the uninitiated PHP developers would never try to make their hands dirty and feet wet with code and will depend on them forever.
So the first step is start believing in your potentials, powers and strength of your mind. Coding is like writing. I show you how easy it is!
Consider a piece of writing where you’re trying to describe a robot. So in your writing ‘robot’ is the subject. In object oriented programming the ‘robot’ is the object. In your piece of writing, your subject ‘robot’ has some descriptions like – the robot has three eyes, two noses, six arms.
In OOP we need to write a class ‘Robot’ which has those properties like this:

//Code/php7book1/index.php
arm = $arms;
        $this->eye = $eyes;
        $this->nose = $noses;
        echo "The robot has {$eyes} eyes, {$noses} noses, {$arms} arms";
    }   
  }
//class ended
$robot = new Robot();
$robot->showRobot(3, 2, 6);

When you run this script, the output is as expected: The robot has 3 eyes, 2 noses, 6 arms.
All we have done is very simple – we wrote a Robot class where we have defined properties and methods. Next, we have created a robot object and through which we have put some life into it. The robot has jumped into life and says it has 3 eyes, 2 noses and 6 arms.
Is it okay? Apparently, it’s definitely okay. But, there could be some problems. Suppose somebody changes the last line of code.

$robot->showRobot(3, two, 6);

Look, instead of passing an integer the user passes a string. Well, php will tackle it by its own way. It’ll give you an output like this: The robot has 3 eyes, two noses, 6 arms.
But, we intended to give an integer, isn’t it?
In PHP 7 this problem has been solved. The scalar type declaration has made it mandatory to pass integer when integer value has been asked for.
Let us write our old code this way.

//Code/php7book1/index.php
arm = $arms;
        $this->eye = $eyes;
        $this->nose = $noses;
        echo "The robot has {$eyes} eyes, {$noses} noses, {$arms} arms";
    }   
  }
//class ended
$robot = new Robot();
$robot->showRobot(3, 2, 6);

The change has taken place only on this line:

public function showRobot(int $eyes, int $noses, int $arms)

Here we have mentioned, categorically, that the parameters should be passed as integers. Now, if anybody passes string or any thing other than integer it’ll throw errors.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s