Object Oriented

Class

PHP

 

Defining Class

PHP 5
class Person {
  
  public $name;
  public $age;
  
  public function greet() {
    print("Hi! I'm " . $this->name);
  }
  
}

Class is a blueprint from which Objects are created.

Class represents a set of properties (state) and methods (behaviors) that are common to all objects created from it.

 

Instantiate Class

PHP 5
$person1 = new Person();

$person2 = new Person();

new keyword creates a new object out of class.

Object is giving a reality to a class.

Class is like a blueprint for a house and Object is like an actual house created from that blueprint.