溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP中如何使用魔術(shù)常量

發(fā)布時(shí)間:2021-06-30 16:32:39 來源:億速云 閱讀:124 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)PHP中如何使用魔術(shù)常量,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

  PHP魔術(shù)方法有哪些

  __construct(),類的構(gòu)造函數(shù)__destruct(),類的析構(gòu)函數(shù)__call(),在對(duì)象中調(diào)用一個(gè)不可訪問方法時(shí)調(diào)用__callStatic(),用靜態(tài)方式中調(diào)用一個(gè)不可訪問方法時(shí)調(diào)用__get(),獲得一個(gè)類的成員變量時(shí)調(diào)用__set(),設(shè)置一個(gè)類的成員變量時(shí)調(diào)用__isset(),當(dāng)對(duì)不可訪問屬性調(diào)用isset()或empty()時(shí)調(diào)用__unset(),當(dāng)對(duì)不可訪問屬性調(diào)用unset()時(shí)被調(diào)用。__sleep(),執(zhí)行serialize()時(shí),先會(huì)調(diào)用這個(gè)函數(shù)__wakeup(),執(zhí)行unserialize()時(shí),先會(huì)調(diào)用這個(gè)函數(shù)__toString(),類被當(dāng)成字符串時(shí)的回應(yīng)方法__invoke(),調(diào)用函數(shù)的方式調(diào)用一個(gè)對(duì)象時(shí)的回應(yīng)方法__set_state(),調(diào)用var_export()導(dǎo)出類時(shí),此靜態(tài)方法會(huì)被調(diào)用。__clone(),當(dāng)對(duì)象復(fù)制完成時(shí)調(diào)用

  __construct()和__destruct()

  構(gòu)造函數(shù)和析構(gòu)函數(shù)應(yīng)該不陌生,他們?cè)趯?duì)象創(chuàng)建和消亡時(shí)被調(diào)用。例如我們需要打開一個(gè)文件,在對(duì)象創(chuàng)建時(shí)打開,對(duì)象消亡時(shí)關(guān)閉

  classFileRead

  {

  protected$handle=NULL;

  function__construct(){

  $this->handle=fopen(...);

  }

  function__destruct(){

  fclose($this->handle);

  }

  }

  >

  這兩個(gè)方法在繼承時(shí)可以擴(kuò)展,例如:

  classTmpFileReadextendsFileRead

  {

  function__construct(){

  parent::__construct();

  }

  function__destruct(){

  parent::__destruct();

  }

  }

  >

  __call()和__callStatic()

  在對(duì)象中調(diào)用一個(gè)不可訪問方法時(shí)會(huì)調(diào)用這兩個(gè)方法,后者為靜態(tài)方法。這兩個(gè)方法我們?cè)诳勺兎椒?Variablefunctions)調(diào)用中可能會(huì)用到。

  classMethodTest

  {

  publicfunction__call($name,$arguments){

  echo"Callingobjectmethod'$name'".implode(',',$arguments)."\n";

  }

  publicstaticfunction__callStatic($name,$arguments){

  echo"Callingstaticmethod'$name'".implode(',',$arguments)."\n";

  }

  }

  $obj=newMethodTest;

  $obj->runTest('inobjectcontext');

  MethodTest::runTest('instaticcontext');

  >

  __get(),__set(),__isset()和__unset()

  當(dāng)get/set一個(gè)類的成員變量時(shí)調(diào)用這兩個(gè)函數(shù)。例如我們將對(duì)象變量保存在另外一個(gè)數(shù)組中,而不是對(duì)象本身的成員變量

  classMethodTest

  {

  private$data=array();

  publicfunction__set($name,$value){

  $this->data[$name]=$value;

  }

  publicfunction__get($name){

  if(array_key_exists($name,$this->data))

  return$this->data[$name];

  returnNULL;

  }

  publicfunction__isset($name){

  returnisset($this->data[$name])

  }

  publicfunctionunset($name){

  unset($this->data[$name]);

  }

  }

  >

  __sleep()和__wakeup()

  當(dāng)我們?cè)趫?zhí)行serialize()和unserialize()時(shí),會(huì)先調(diào)用這兩個(gè)函數(shù)。例如我們?cè)谛蛄谢粋€(gè)對(duì)象時(shí),這個(gè)對(duì)象有一個(gè)數(shù)據(jù)庫鏈接,想要在反序列化中恢復(fù)鏈接狀態(tài),則可以通過重構(gòu)這兩個(gè)函數(shù)來實(shí)現(xiàn)鏈接的恢復(fù)。例子如下:

  classConnection

  {

  protected$link;

  private$server,$username,$password,$db;

  publicfunction__construct($server,$username,$password,$db)

  {

  $this->server=$server;

  $this->username=$username;

  $this->password=$password;

  $this->db=$db;

  $this->connect();

  }

  privatefunctionconnect()

  {

  $this->link=mysql_connect($this->server,$this->username,$this->password);

  mysql_select_db($this->db,$this->link);

  }

  publicfunction__sleep()

  {

  returnarray('server','username','password','db');

  }

  publicfunction__wakeup()

  {

  $this->connect();

  }

  }

  >

  __toString()

  對(duì)象當(dāng)成字符串時(shí)的回應(yīng)方法。例如使用echo$obj;來輸出一個(gè)對(duì)象

  //Declareasimpleclass

  classTestClass

  {

  publicfunction__toString(){

  return'thisisaobject';

  }

  }

  $class=newTestClass();

  echo$class;

  >

  這個(gè)方法只能返回字符串,而且不可以在這個(gè)方法中拋出異常,否則會(huì)出現(xiàn)致命錯(cuò)誤。

  __invoke()

  調(diào)用函數(shù)的方式調(diào)用一個(gè)對(duì)象時(shí)的回應(yīng)方法。如下

  classCallableClass

  {

  function__invoke(){

  echo'thisisaobject';

  }

  }

  $obj=newCallableClass;

  var_dump(is_callable($obj));

  >

  __set_state()

  調(diào)用var_export()導(dǎo)出類時(shí),此靜態(tài)方法會(huì)被調(diào)用。

  classA

  {

  public$var1;

  public$var2;

  publicstaticfunction__set_state($an_array){

  $obj=newA;

  $obj->var1=$an_array['var1'];

  $obj->var2=$an_array['var2'];

  return$obj;

  }

  }

  $a=newA;

  $a->var1=5;

  $a->var2='foo';

  var_dump(var_export($a));

  >

  __clone()

  當(dāng)對(duì)象復(fù)制完成時(shí)調(diào)用。例如在設(shè)計(jì)模式詳解及PHP實(shí)現(xiàn):?jiǎn)卫J揭晃闹刑岬降膯卫J綄?shí)現(xiàn)方式,利用這個(gè)函數(shù)來防止對(duì)象被克隆。

  publicclassSingleton{

  privatestatic$_instance=NULL;

  //私有構(gòu)造方法

  privatefunction__construct(){}

  publicstaticfunctiongetInstance(){

  if(is_null(self::$_instance)){

  self::$_instance=newSingleton();

  }

  returnself::$_instance;

  }

  //防止克隆實(shí)例

  publicfunction__clone(){

  die('Cloneisnotallowed.'.E_USER_ERROR);

  }

  }

  >

  PHP魔術(shù)常量怎么使用

  PHP中的常量大部分都是不變的,但是有8個(gè)常量會(huì)隨著他們所在代碼位置的變化而變化,這8個(gè)常量被稱為魔術(shù)常量。

  __LINE__,文件中的當(dāng)前行號(hào)__FILE__,文件的完整路徑和文件名__DIR__,文件所在的目錄__FUNCTION__,函數(shù)名稱__CLASS__,類的名稱__TRAIT__,Trait的名字__METHOD__,類的方法名__NAMESPACE__,當(dāng)前命名空間的名稱

  這些魔術(shù)常量常常被用于獲得當(dāng)前環(huán)境信息或者記錄日志。

關(guān)于PHP中如何使用魔術(shù)常量就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI