Constructor and Destructor in PHP
by TechVidvan Team
Program 1
<html>
<head><title>Demo</title></head>
<body>
<center>
<?php
class Test
{
public $mycon=null;
public function __construct()
{
//echo "This is a constructor.<br>";
$this->mycon=mysqli_connect("localhost","root","","Library") or die("Connection not success");
echo "Connection open success<br>";
}
public function display()
{
echo "This is a display function.<br>";
}
public function show()
{
echo "This is a show function.<br>";
}
public function __destruct()
{
// echo "This is a desctuctor.<br>";
$this->mycon->close();
}
}
$T=new Test();
$T->display(); //explicit
$T->display();
$T->show();
$T->display();
?>
</center>
</body>
</html>
Tags: constructor and destructor in phpconstructor in phpdestructor in phpPHPphp constructorphp constructor and destructorphp destructorphp practicalphp program
TechVidvan Team
TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.