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;
}
?>
- <?php
- Example:
- <?php
class Msg {
echo “Hello India”;
}
?> - Click this link for full Example details.
- <?php
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;
}
- <?php
$ObjectName=new ClassName();
-
- Example:
- Click this link for full Example details.
- Example:
‘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:
- Click this link for full Example details.
‘instanceof’ keyword
-
- The ‘instanceof’ keyword is used to check that an object belongs to a specific class.
- Example:
- Click this link for full Example details.
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.
- Public :
0 Comments