![Php object-oriented scripting language in 2021 Php object-oriented scripting language in 2021](https://blogger.googleusercontent.com/img/a/AVvXsEiQgYPoNwXNSDEq0z8gYrf3MxGo8DIWLDpNJYdvz5_RIhI9-P6xE3s4ZEfRsblqEzPdqXg3M6CP__k5Kp0bjT50DKtcoWwELMzF87X2RdiX4luWhv_NfGrgDl-enAflXoypCHNa9Lr_Jr0hTGvWIWkK2AjgkX0qx9RbN5odwg4UQvooUAvbc7nOdO0ccw=w640-h360)
Php object-oriented scripting language in 2021
Php object-oriented scripting language
PHP Object-Oriented: PHP is a server-side scripting language that allows you to create dynamic web pages. This means that when a user visits your website, it will dynamically generate content based on the user's request.
This introduction has given very detailed information about how PHP can be used in an object-oriented way.
Object in PHP: A Object is having states and behaviors in which state means what does it has and behavior means what does it do.
The object is an instance of the class. We
can create many objects of a single class. An object of a class is created using a new
keyword.
Class in PHP: Class is a collection of data members and member functions. Data members are the variable used inside the class. Member functions are the function used inside the class. is also called a User-defined data type.
Syntax of Class in PHP
Class class_name { Data member1; Data member2; Data member3; -------------------- Member function1; Member function2; Member function3; } |
Class Declaration: Most points to
remember while making class program in PHP.
1. Declare variable with appropriate access specifier
as per your requirement
for example to find the area of rectangle three variables are sufficient(height,
width, and area)
2. Declare the function as per your requirement
for example to find the area of rectangle i am using three
function set_height($h),set_width($w) and find_area().
How to use class declaration in PHP?
<?php class Rectangle { public $height; public $width; public $area; function set_height($h) { $this->height = $h; } function set_width($w) { $this->name=$w; } function find_area() { $this->area=$this->height*$this->width;
echo "Area of
Rectangle=".$this->area; } } ?> |
this
keyword: $this keyword
refers to the current object of the class. It
can be used only inside methods. By using $this keyword
we can pass the local variable value into the class-level variable.
Defining
Object: Create object of class
to access the data member and member function of a class. new keyword is used to create object.
objectname=new classname();
$rec1=new Rectangle();
Here Rectangle is the name of the class and rec1 is the name of the object.
Accessing data member and member function. Data
member and member function of a class can be accessed
using the arrow(->)operator.
1. Accessing
data member
rec1->set_height(12);
rec1->set_width(12);
2. Accessing
member function
rec1->find_area();
<?php class Rectangle { public $height; public $width; public $area; function set_height($h) { $this->height = $h; } function set_width($w) { $this->width=$w; } function find_area() { $this->area=$this->height*$this->width;
echo "Area of Rectangle:".$this->area;
} } $r=new Rectangle(); $r->set_height(12); $r->set_width(12); $r->find_area(); ?> *****OUTPUT***** Area of
Rectangle:144 |
Accessing
Data member Outside a class: A Class level variable
can be accessed outside a class by using the object.
How to access data members outside of a class in PHP?
<?php class Rectangle { public $height; public $width; public $area; function set_height($h) { $this->height = $h; } function set_width($w) { $this->width=$w; } function find_area() { $this->area=$this->height*$this->width;
echo "Area of
Rectangle=".$this->area; } } $r=new Rectangle(); $r->set_height(100); $r->set_width(200); echo "Rectangle Height:".$r->height."<br>";
echo"Rectangle Width:". $r->width; ?> *****OUTPUT***** Rectangle Height:100 Rectangle Width:200 |
How to change the property value in PHP?
<?php class Rectangle { public $height; public $width; public $area; function set_height($h) { $this->height = $h; } function set_width($w) { $this->width=$w; } } $r=new Rectangle(); $r->set_height(100); $r->set_width(200); $r->height=50; echo $r->height."<br>"; echo $r->width; ?> *****OUTPUT***** 50 200 because 100 is replaced with 50 |