What is class in php explain it with example?


A class is a template for objects, and an object is an instance of class.


OOP Case

Let's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.

When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.


Define a Class

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:

Syntax

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

Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:

<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>

Note: In a class, variables are called properties and functions are called methods!


Define Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Objects of a class is created using the new keyword.

In the example below, $apple and $banana are instances of the class Fruit:

Example

<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}

$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');

echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>

Try it Yourself »

In the example below, we add two more methods to class Fruit, for setting and getting the $color property:

Example

<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
  function set_color($color) {
    $this->color = $color;
  }
  function get_color() {
    return $this->color;
  }
}

$apple = new Fruit();
$apple->set_name('Apple');
$apple->set_color('Red');
echo "Name: " . $apple->get_name();
echo "<br>";
echo "Color: " . $apple->get_color();
?>

Try it Yourself »



PHP - The $this Keyword

The $this keyword refers to the current object, and is only available inside methods.

Look at the following example:

Example

<?php
class Fruit {
  public $name;
}
$apple = new Fruit();
?>

So, where can we change the value of the $name property? There are two ways:

1. Inside the class (by adding a set_name() method and use $this):

Example

<?php
class Fruit {
  public $name;
  function set_name($name) {
    $this->name = $name;
  }
}
$apple = new Fruit();
$apple->set_name("Apple");

echo $apple->name;
?>

Try it Yourself »

2. Outside the class (by directly changing the property value):

Example

<?php
class Fruit {
  public $name;
}
$apple = new Fruit();
$apple->name = "Apple";

echo $apple->name;
?>

Try it Yourself »


PHP - instanceof

You can use the instanceof keyword to check if an object belongs to a specific class:

Example

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

Try it Yourself »



View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like C++ and Java, PHP also supports object oriented programming

    1. Classes are the blueprints of objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: ‘object’.
    2. Class is a programmer-defined data type, which includes local methods and local variables.
    3. Class is a collection of objects. Object has properties and behavior.

    Syntax: We define our own class by starting with the keyword ‘class’ followed by the name you want to give your new class.

    <?php 
        class person {
     
        }
    ?>

    Note: We enclose a class using curly braces ( { } ) … just like you do with functions.

    Given below are the programs to elaborate the use of class in Object Oriented Programming in PHP.
    The programs will illustrate the examples given in the article.

    Program 1:

    <?php

    class GeeksforGeeks

    {

        public function __construct(){

            echo 'The class "' . __CLASS__ . '" was initiated!<br>';

        }

    }

    $obj = new GeeksforGeeks;

    ?>

    Output:

    The class "GeeksforGeeks" was initiated.

    Program 2:

    <?php

    class GeeksforGeeks

    {

        public function __destruct(){

            echo 'The class "' . __CLASS__ . '" was destroyed!';

        }

    }

    $obj = new GeeksforGeeks;

    ?>

    Output:

    The class "GeeksforGeeks" was destroyed.
    

    Reference:
    Classes in PHP


    What is class in PHP with example?

    Class − This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object. Object − An individual instance of the data structure defined by a class.

    What is the class in PHP?

    Class is a programmer-defined data type, which includes local methods and local variables. Class is a collection of objects. Object has properties and behavior.

    What is class and explain with example?

    A class is a group of objects that share common properties and behavior. For example, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc. And its behavior is mobility.

    What is class in oops with example?

    A class is a way of organizing information about a type of data so a programmer can reuse elements when making multiple instances of that data type—for example, if a programmer wanted to make three instances of Car , maybe a BMW, a Ferrari, and a Ford instance.

    Tải thêm tài liệu liên quan đến bài viết What is class in php explain it with example?