一:
PHP爲我們提供了一系列用__開頭的函數,這些函數無需自己動手調用,會在何時的時機自動調用,稱這類函數爲魔術函數。
如:
function __construct( ) {} 在new一個新對象時自動調用此函數。
二:
1:構造函數__construct()和析構函數_destruct()
在new新的Player對象時會調用__construct()函數。對象銷燬前自動調用__destruct函數。
代碼如下:
<?php
class Player{
public $name;
public $age;
function __construct( $name, $age ){
echo "構造函數<br>";
$this->name = $name;
$this->age = $age;
}
function getName( ){
return $this->name;
}
function __destruct( ){
echo "析構函數<br>";
}
}
$player_1 = new Player( "kobe", 36 );
$name = $player_1->getName( );
echo $name;
echo "<br>";
unset($player_1);
echo "end";
?>
運行結果:
2:__get():訪問類中私有屬性時自動調用,傳遞讀取屬性名,返回$this->屬性名。
當直接訪問私有成員變量echo $player_1->name;時,會有報錯信息,顯示不能調用name。
使用__get可以訪問私有屬性。
<?php
class Player{
private $name;
function __construct( $name ){
echo "構造函數<br>";
$this->name = $name;
}
function __get( $name ){
return $this->$name;
}
}
$player_1 = new Player( "kobe", 36 );
echo $player_1->name;
echo "<br>";
?>
運行結果如下:
3:__set():給類的私有屬性賦值時調用,傳遞需設置的屬性名。屬性值。
當設置私有成員變量$player_1->name = “james”;時,會有報錯信息。
正確打開方式是:
<?php
class Player{
private $name;
private $age;
function __construct( $name ){
echo "構造函數<br>";
$this->name = $name;
}
function __set( $key, $value ){
$this->$key = $value;
}
function __get( $name ){
return $this->$name;
}
}
$player_1 = new Player( "kobe", 36 );
$player_1->name = "james";
echo $player_1->name;
echo "<br>";
?>
運行結果如下:
4:__isset():和 __unset():
__isset():檢測對象私有屬性時調用,傳遞檢測的屬性名,返回isset($this->屬性名)。
__unset():使用unset函數刪除對象的私有屬性時調用,傳遞刪除的屬性名。方法中執行unset($this->屬性名)。
unset私有成員變量時,會有報錯信息。
代碼如下:
<?php
class Player{
private $name;
private $age;
function __construct( $name ){
echo "構造函數<br>";
$this->name = $name;
}
function __isset( $key ){
return isset( $this->$key);
}
function __unset( $key ){
unset( $this->$key );
}
}
$player_1 = new Player( "kobe", 36 );
if ( isset($player_1->name) )
echo "name 成員變量存在";
else
echo "name 成員變量不存在";
unset($player_1->name);
echo "<br>";
?>
5:__toString():使用echo打印對象時調用,返回打印對象時想要顯示的內容,返回必須是字符串。
echo $player_1;時會有報錯信息。
代碼如下:
<?php
class Player{
private $name;
function __construct( $name ){
echo "構造函數<br>";
$this->name = $name;
}
function __toString(){
return "顯示對象";
}
}
$player_1 = new Player( "kobe" );
echo $player_1;
echo "<br>";
?>
運行結果:
6:__call():調用一個類中未定義的方法或者私有、受保護的方法時自動調用__call函數。傳遞被調用的函數名及參數列表。
<?php
class Player{
public $name;
function __construct( $name ){
echo "構造函數<br>";
$this->name = $name;
}
private function getName(){
return $this->$name;
}
function __call( $funcName, $funcParams ){
echo "調用的函數是$funcName, 參數列表是:";
print_r( $funcParams );
}
}
$player_1 = new Player( "kobe" );
echo $player_1->setName( "james" );
echo "<br>";
echo $player_1->getName();
echo "<br>";
?>
7:__clone():當使用clone關鍵字克隆一個對象時自動調用,作用是爲新克隆的對象初始化賦值。
<?php
class Player{
public $name;
function __construct( $name ){
echo "構造函數<br>";
$this->name = $name;
}
function __clone( ){
$this->name = "james";
}
}
$player_1 = new Player( "kobe" );
echo $player_1->name . "<br>";
$player_2 = clone $player_1;
echo $player_2->name . "<br>";
echo "<br>";
?>
運行結果如下:
8:__sleep():對象序列化時自動調用,返回一個數組,數組中的值就是可以序列化的屬性。可以定義serialize()序列化時返回的數據。
不使用__sleep時,serialize()返回類中所有的成員變量數據。
使用__sleep之後的代碼如下:
<?php
class Player{
public $name;
public $age;
public $sex;
function __construct( $name, $age, $sex ){
echo "構造函數<br>";
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
function __sleep( ){
return array("name", "sex");
}
}
$player_1 = new Player( "kobe", 35, "male" );
echo serialize($player_1);
?>
運行結果如下:
9:__wakeup():對象反序列化時調用,爲反序列化新產生的對象進行初始化賦值。
代碼如下:
<?php
class Player{
public $name;
public $age;
public $sex;
function __construct( $name, $age, $sex ){
echo "構造函數<br>";
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
function __wakeup( ){
$this->name = "kobe";
$this->age = 35;
$this->sex = true;
}
}
$player_1 = new Player( "kobe", 22, false );
$temp = serialize($player_1);
$player_2 = unserialize($temp);
var_dump($player_2);
?>
運行結果: