Introduction

  • Class and Object are one of the most important features of OOPs.

Definition

Class

    • A class is simply a template for objects.
    • A class is created by using the ‘class’ keyword, followed by the name of the class and a pair of curly braces ({}). All the properties/variables and methods/functions are written inside these braces.
    • A class may be one or more objects as per need.
    • Syntax:
      • <?php
        class ClassName {
          // single or multiple source codes statements;
        }
        ?>
    • Example:

Object

    • An object is an instance of a class.
    • At least one object is necessary for a class i.e., there is no meaning of a class without an object.
    • Objects are created using the ‘new’ keyword.
    • When an individual object is created for a class then the created object inherits all the properties and behaviors templates of the class, but each object has its own (different) values of the properties.
    • The properties and methods of a class are called using object name -> (operator) and then properties or methods name. 
    • Syntax:
      • <?php
        class ClassName {
          // single or multiple source codes statements;

   $ObjectName=new ClassName();

?>

‘this’ keyword

    • The ‘this’ keyword in Php is used to represent the current object.
    • ‘this’ keyword is only applicable inside methods of a class.
    • Example:

‘instanceof’ keyword

Access Specifier or Modifiers in Php

  • Access Specifier is a way to control the object to access the properties and methods of a class.
  • There are three access modifiers in Php:-
    • Public : 
      • In this access specifier, the property or method can be accessed directly by an object of the same or different class.
      • This specifier has no security.
      • This is the default access specifier in Php.
    • Protected :
      • In this access specifier, the property or method can be accessed by an object of the same class or derived class.
    • Private :
      • In this access specifier, the property or method can only be accessed by an object of the same class.
      • This specifier has the highest security.

    Loading

    Categories: Php Theory

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.