PHP OOP




OOP stands for Object-Oriented Programming. Object-oriented programming is about creating objects that include each knowledge and features, that is completely different from Procedural programming which is about writing procedures or features that carry out operations on the information.



Why OOP?
  • OOP is quicker and simpler to execute
  • OOP gives a transparent construction for the applications
  • OOP helps to maintain the PHP code DRY “Do not Repeat Your self”, and
    makes the code simpler to keep up, modify and debug
  • OOP makes it attainable to create full reusable functions with
    much less code and shorter development time


Courses & Objects

Courses and objects are the 2 most important elements of object-oriented programming.

For instance

So, a category is a template for objects, and an object is an occasion of a category.

When the person objects are created, they inherit all of the properties and behaviors from the category, however every object could have completely different values for the properties.



Class

Let assume we have now a category Particular person, an individual has a reputation, age and gender.
To outline a category, use curly braces.In a category, variables are known as properties and features are known as strategies!

<?php
class Particular person{ 
 //properties of the category
 public identify;
 public age;
 public gender;
 //strategies of the category
 operate setName(identify){
   $this->identify = identify;
 }
 operate getName(){
   return $this->identify;
 }
 operate setAge(identify){
   $this->identify = identify;
 }
 operate getAge(){
   return $this->identify;
 }
 operate setGender(identify){
   $this->identify = identify;
 }
 operate getGender(){
   return $this->identify;
 }


}
?>


Objects

Courses are nothing with out objects! We are able to create a number of objects from a category. Every object has all of the properties and strategies outlined within the class, however they are going to have completely different property values.

Objects of a category is created utilizing the new key phrase.

<?php
$person1 = new Particular person()
$person1->setName('samuel')
echo "Identify:" .$person1->getName()
?>


The $this Key phrase

The $this key phrase refers back to the present object, and is just accessible inside strategies.



PHP – instanceof

You should utilize the instanceof key phrase to verify if an object belongs to a selected class:

<?php
$apple = new Fruit();
var_dump($Person1 instanceof Particular person);
?>



Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.


Checkout extra Articles on Sayed.CYou

#PHP #OOP