Constructor and Destructor in PHP

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>

 

courses

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.

Leave a Reply

Your email address will not be published. Required fields are marked *