![How to use constractor in php with example 2021 How to use constractor in php with example 2021](https://blogger.googleusercontent.com/img/a/AVvXsEhy_eNX9_o23TII86w1WnkmkhTGl46LL2t6I6Ok1_-LGxAD91YPskFJM5lYISNMGr_pX_wq7It62q2LDgu_GAhjKQq1XEjBcBIvarOzdRPHidm70fDB90hSmcR_8I0g61NMs6FOqxjsbSg12S7d2endiTLE5es_h067JcQwPSMv8_tJMVGEzcsNrX-r0A=w640-h360)
How to use a constructor in PHP with example 2021
How to use a constructor in PHP with example
PHP Contractor: PHP is a server-side web programming language that is often used as a replacement for ASP, JSP, and ASP.NET.
It allows you to create dynamic web pages by executing code on the server-side. In this article, we will show you how to use a PHP constructor to generate your HTML pages dynamically using the server-side scripting language.
The steps are as follows:
1. Create an HTML file with the following content:
2. Insert PHP tags around variables and values which you want to dynamically change:
3. Include a PHP script at the top of your file:
4. This is my dynamically generated page!";?
Constructor: It is a special member function of a class that executes when we create the instance(object) of that class. in other words, we can say that there is no need to call a constructor. PHP provides a special function called __construct() to define a constructor. construct() function start with two underscore. It may be parameterized or non-parameterized.
How to use Constructor without parameters in PHP?
<?PHP class Rectangle { public $h; public $w; public $area; function __construct() { $this->h = 20; $this->w = 20; $this->area = $this->h*$this->w; echo "Area of Rectangle:".$this->area; } } $r=new Rectangle(); ?> *****OUTPUT***** Area of Rectangle:400 |
How to use Constructor with parameter in PHP?
<?php class Rectangle { public $height; public $width; public $area; function __construct($h,$w) { $this->height = $h; $this->width = $w; $this->area = $this->height*$this->width; echo "Area of Rectangle:".$this->area; } } $r=new
Rectangle(20,20); *****OUTPUT***** Area of Rectangle:400 |
Destructor: It is also a special member function of a class that executes when the work of an object goes out of scope. PHP provides a special function called __destruct() to define a destructor. destruct() function starts with two underscores. Destructor is called at the end of the script.
How to use Constructor with parameters in PHP?
<?PHP class Intro { public $name; public $marks; function __construct($n,$m) { $this->name = $n; $this->marks = $m; } function __destruct() { echo "Student
Name:".$this->name."<br>"; echo "Student Marks:".$this->marks; } } $student = new Intro("Icoderweb",2022); ?> Student Name:Icoderweb Student Marks:2022 |