SoFunction
Updated on 2025-03-10

Simple examples of php initialization objects and destructors


<?php
 /**********************************************
*  __construct  Object initialization function use
* destruct        Use of destructors
*  $this                                                                                                                            �
 * 
 **********************************************/
 header("Content-Type:text/html;charset=UTF-8");
 class mypc{
  public $name;
  public $type;

function __construct($name='',$type=''){ //Initialize the object and put the initialization value in brackets
   $this->name=$name;
   $this->type=$type;

  }
  function vod(){
return $this->name.$this->type.'Play movie';
  }
  function game(){
return $this->name.$this->type.'Play the game';
  }
  /************************
* When the operation inside the object is executed,
* __destruct() is called,
* Then the memory used by the object is released. Rules: After entering, first exit
  **************************/
  function __destruct(){
   echo "<br>==============".$this->name;
  }
 }

$pc1 = new mypc('home computer','desktop');
 echo $pc1->vod()."<br>";
//When $pc1=null;, the object is directly released after the current instance operation is completed
$pic2 = new mypc('Company Computer','Notebook');
 echo $pic2->game();