when we talk about any programming, their several ways to program like procedural, object-oriented programming, etc.
What is OOP in PHP?
OOP stands for Object-Oriented Programming.
there are generally two ways to do programming:
- Procedural programming is about writing procedures or functions that perform operations on the data,
- Object-oriented programming is about creating objects containing data and functions.
Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- with OOP there is easy maintenance and debug
- OOP makes development possible to create full reusable applications with less code
- OOP provides a shorter development time
there are two main concepts in OOP'S:
- objects
- classes
CLASS:
A class is a blueprint of objects. A class is created using the class keyword.
syntax:
class Class_name
{
#block of code
}
for Example: let's create a class named 'students'.
class Students
{
$rollno=10030;
}
OBJECTS:
It is a real-world entity that has its own property and behavior.
how to create an object?
to create an object of any class you have to use new keyword.
syntax:
$object_name=new Class_name();
example:
let's create some objects of class 'students'
<?php
class Students
{
public $category="insan";
public $rollno=10034;
function reading()
{
echo "student is reading";
}
}
$om=new Students();
echo $om->rollno;
$om->reading();
?>
In above example:
class : Students
object : $om
properties: rollno, category
behaviors : reading
0 Comments