Php Object Oriented Programming
Object Oriented programming is a programming language structure that is associated with the concept of OBJECTS, data fields, method and related member functions. Its to organize the data and structure of an php application.
Now a day without OOP we cannot move a inch in PHP world. All PHP base Framework are established on OOP php language like as – Laravel, CI, Zend.
Countless evade the utilization of OOP as a programming worldview, numerous more are seeing the benefits of OOP consistently. Items are setting down deep roots – and on the off chance that you don’t care for them yet, give it a chance: there comes a period in each engineers profession when he makes the transformative stride up to OOP.
OOPS Elements Details:
- Class
- Object
- Inheritance,
- Aggregation,
- Composition,
- Encapsulation
- Polymorphism
These are known as fundamentals of Object Oriented Programming.
What and do Class:
It is like structures in C programming. Class can likewise be characterized as defined data type however it additionally contains functions in it. In this way, class is essentially an outline for object. It declare and defines what information/data variables the Object will have and what operations can be performed on the object of class.
Elements of OOPS Class:
==> Object : Objects have states and behaviors.
Objects are the basic element of OOP. Objects are instances of class, which have data members and uses various member functions to execute their tasks.
Example: A Person has States- color, name, nation and so on. Behaviors – Working, walking, and eating. An object is an instance of a class.
==> Methods – A method is basically a behavior. Class can contain many methods. It is in methods where the logic are written, data is manipulated and all the actions are run.
How to Work OOP Class :
First Create a php file named - myclass.php and keep it into server.
1 2 3 4 5 6 7 8 |
class Myclass{ public $description = "Welcome to Object Oriented Programming" ; } $mydetail = new Myclass(); var_dump($mydetail->description); |
Details Example using Constructor and Function Method :
12345678910111213141516171819202122232425262728293031323334353637383940 class Myclass{public $description;public $completed = false;public function __construct($description) {$this->description = $description;}public function completed(){$this->completed = true;}}$mydetail = new Myclass("Welcome to Object Oriented Programming");var_dump($mydetail->description);//Result Display : string(38) "Welcome to Object Oriented Programming"// NULL ( var_dump($mydetail->description); )$object2 = new Myclass("What is object ?");echo "";var_dump($object2->description);// string(16) "What is object ?" var_dump($object2->description);echo "";var_dump($object2->completed);// bool(false)//Result Display : string(38) “Welcome to Object Oriented Programming”
//string(16) “What is object ?”
Another way to work OOPS Class with Encapsulation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Person{ public $name; public $age; public function __construct($name) { $this->name = $name; } public function getAge(){ return $this->age * 365; } public function setAge($age){ echo $this->name; if($age < 18){ echo " This Person is Very Young"; } else { echo " This Person is Young so Far"; } } // setAge End } $person = new Person("Mr. XYZ PQR"); $person->setAge(30); $person->age = 8; echo " " .$person->getAge(); |
Display Result after Run person.php Code
Mr. XYZ PQR This Person is Young so Far
2920
Working on OOPS Inheritance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php class shape { //put your code here public $color; public function __construct($color = 'red') { $this->color = $color; } public function getColor(){ return $this->color; } } class Square extends shape{ protected $length = 9; public function getArea(){ return pow($this->length, 2); } } class Traingle extends shape{ public $base = 4; protected $height = 7; public function getArea(){ return .5 * $this->base * $this->height; } } class Circle extends shape{ public $redius = 5; public function getArea(){ return M_PI * pow($this->redius , 2); } } $traingle = new Traingle(); echo $traingle->getArea() . "<br>"; //$circle = new shape("RED"); //echo "Color Is : " .$circle->color; echo "Color is : " .(new Square('Green'))->getColor() . "<br>"; echo "Area of Circle : " . (new Circle())->getArea(); ?> |