PHP OOP - Classes and Objects

PHP OOP - Classes and Objects


An object is an instance of a class, which serves as a template for other objects.


OOP Case

Let us suppose we have such a Fruit class. A Fruit can have attributes such as a name, colour, weight, and so on. To store the values of these properties, we can declare variables such as $name, $colour, and $weight.

They inherit the class properties and behaviours when they are generated as individual objects (apple, banana, etc.), but each object will have a unique set of attributes.

You can also search for these topics, php oop case, how to hold values in php oop case, php properties oop case.

Create or Define a PHP Class

When defining a class, the class keyword is used, followed by the class name and a set of curly braces({}). They contain all of its characteristics and methods :

Syntax :-

<?php
class class_name {
  // code goes here...
}
?>

Example 1 :- There is a property ($name) in the Fruit class, as well as set_name() and get_name() for setting and retrieving $name, respectively :

<?php
class Fruit 
{
  // Properties
  public $name;
  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>

Note : Variables are referred to as properties in a class, and functions are referred to as methods!

A class can have number of properties and methods.

All names (class, property, method) are user defined.

Example 2 :- There are three properties ($name, $dept, and $salary) in the Employee class, as well as there are six methods for setting and retrieving, their values respectively.

<?php
class Employee 
{
  // Properties
  public $name, $dept, $salary;
  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
  function set_dept($dept) {
    $this->dept = $dept;
  }
  function get_dept() {
    return $this->dept;
  }
  function set_salary($salary) {
    $this->salary = $salary;
  }
  function get_salary() {
    return $this->salary;
  }
}
?>

Example 3 :- There are two properties ($a and $b) in the Calculator class, as well as there are four methods for making operations like addition and subtraction, respectively.

<?php
class Calculator 
{
  // Properties
  public $a, $b;
  // Methods
  function setA($a) {
    $this->a = $a;
  }
  function setB($b) {
    $this->b = $b;
  }
  function addAB()
  {
    $c = $this->a + $this->b;
    echo "Addition is : " . $c;
  }
  function subAB()
  {
    $c = $this->a - $this->b;
    echo "Subtraction is : " . $c;
  }
}
?>


You can also search for these topics, php class name, how to define a php class, define a php class variables, syntax for php class, list the properties in php class, php class exists, variables and method names for functions in a php class, explain class keyword, Example for php define a class.

Create or Define Objects

Classes are meaningless without objects! A class can be used to create many objects. In addition to the properties and methods defined in the class, each object will have a unique set of values for each of its properties.

The new keyword is used to create single object of a class.

Syntax:-

object_name = new class_name();

Example :- Creating 3 objects of Fruit class and Creating 2 objects of Employee class.

<?php
//  Create 3 objects of Fruit class
$apple = new Fruit();
$mango = new Fruit();
$grape = new Fruit();

//  Create 2 objects of Employee class
$john = new Employee();
$rohan = new Employee();
?>

Access Properties and Methods of Class using Object

Now we can see, how to access a class member (propety and method) using an object.

Syntax :-

//  Access a propety
object_name->property_name;
//  Access a method
object_name->method_name();

Example :-

//  $apple from Fruit class
$apple->set_name("apple");  // Calling Method
echo $apple->get_name();    // Calling Method
//  $john from Employee class
$john->name = "john";       // Accessing Property
$john->dept = "sales";      // Accessing Property
$john->set_salary(12000);   // Calling Method
echo $john->get_name();     // Calling Method
echo $john->dept;           // Accessing Property
echo $john->salary;         // Accessing Property

Now we will see a complete program for creating class and objects.

Example :- In the following example, we create a class "Car" with related properties and methods.

<?php
class Car
{
public $name, $price, $color, $fuel;
function setName($name){   
    $this->name = $name;    
}
function setPrice($price){   
    $this->price = $price;    
}
function setColorAndFuel($color, $fuel){   
    $this->color = $color;
    $this->fuel = $fuel;
}
function getCarInfo(){   
    echo "Name = " . $this->name . "<br />";
    echo "Price = " . $this->price . "<br />";
    echo "Color = " . $this->color . "<br />";
    echo "Fuel = " . $this->fuel . "<br />";
}
}
//  We are going to create objects
$BMW = new Car();
$BMW->setName("BMW");
$BMW->setPrice(20000);
$BMW->setColorAndFuel("red", "Petrol");
$BMW->.getCarInfo();
$Volvo = new Car();
$Volvo->setName("Volvo");
$Volvo->setPrice(15000);
$Volvo->setColorAndFuel("black", "Diesel");
$Volvo->.getCarInfo();
?>

Output:-

Name = BMW
Price = 20000
Color = red
Fuel = Petrol

Name = Volvo
Price = 15000
Color = black
Fuel = Diesel



You can also search for these topics, php create multiple objects, new keyword to create php objects, what is php objects, declare the objects in php, php objects to array, define the datatype to php objects, Example for define a objects, properties and methods defined a object.

PHP $this Keyword

The $this keyword is a readymade or built-in object for accessing class members (properties and methods) within inside a class.

It also called as current class object. Only inside methods is the $this keyword available, which relates to the current object.

Note:- In PHP, All class members (except static members) are accessed by object inside or outside the class.