类的定义和实例化
<?php
class NbaPlayer
{
public $name = "Jordan";
public $height = "198cm";
public $weight = "98kg";
public $team = "Bull";
public $playerNumber = "23";
//定义方法
public function run()
{
echo "Running\n";
}
}
$jordan = new NbaPlayer();
echo $jordan->name;
$jordan->run();
?>
构造函数
<?php
//peoples republic of china
date_default_timezone_set("PRC");
class NbaPlayer
{
public $name = "Jordan";
public $height = "198cm";
public $weight = "98kg";
public $team = "Bull";
public $playerNumber = "23";
function __construct($name, $height)
{
$this->name = $name;//this 伪变量
$this->height = $height;
}
//定义方法
public function run()
{
echo "Running\n";
}
public function shoot()
{
echo "shooting\n";
}
}
$jordan = new NbaPlayer('hehe', '220cm');
echo $jordan->name;
$jordan->run();
?>
析构函数
<?php
//peoples republic of china
date_default_timezone_set("PRC");
class NbaPlayer
{
public $name = "Jordan";
public $height = "198cm";
public $weight = "98kg";
public $team = "Bull";
public $playerNumber = "23";
function __construct($name, $height)
{
$this->name = $name;//this 伪变量
$this->height = $height;
}
//定义方法
public function run()
{
echo "Running\n";
}
//在该方法中释放资源
function __destruct()
{
echo "destroying" . $this->name . "\n";
}
}
$jordan = new NbaPlayer('hehe', '220cm');
echo $jordan->name;
$jordan->run();
$jordan = null;//主动调用析构函数 不调用该方法会执行From now on再析构
echo "From now on jordan will not be used.\n";
?>
$jordan = new NbaPlayer('hehe', '220cm');
echo $jordan->name."<br/>";
//$jordan->run();
$jordan = null;//主动调用析构函数 不调用该方法会执行From now on再析构
echo "From now on jordan will not be used.";
对象不再被使用才会触发析构函数
$jordan = new NbaPlayer('hehe', '220cm');
echo $jordan->name."<br/>";
$jordan1=$jordan;
$jordan = null;//主动调用析构函数 不调用该方法会执行From now on再析构
echo "From now on jordan will not be used."."<br/>";
$jordan = new NbaPlayer('hehe', '220cm');
echo $jordan->name."<br/>";
$jordan1=&$jordan;
$jordan = null;//主动调用析构函数 不调用该方法会执行From now on再析构
echo "From now on jordan will not be used."."<br/>";
$jordan = new NbaPlayer('hehe', '220cm');
echo $jordan->name."<br/>";
$jordan1=$jordan;
$jordan2=&$jordan;
$jordan1 = null;//主动调用析构函数 不调用该方法会执行From now on再析构
$jordan2=null;
echo "From now on jordan will not be used."."<br/>";