Sunday, January 14, 2018

OOP's Concept in PHP

OPP (object oriented Programming

 OOP (object oriented Programming), 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! - The Best Popunder Adnetwork

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 a 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 are mentions in a image :- 

OOP PHP code 

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. 

 write a our old code this way :-  

// Code/ php7book1/ index.php 

<? php 

class Robot { 

public $ eye; 

public $ nose; 

public $ arm; 

public function showRobot( int 

$ eyes, int $ noses, int $ arms) { 

$ this-> 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. Read More 


Please comment and follow this site blog inbox