1.內(nèi)部函數(shù):PHP可以在函數(shù)內(nèi)部再聲明函數(shù),目的就是在函數(shù)內(nèi)部調(diào)用,用來幫助外部函數(shù)完成一些子功能.

2.遞歸函數(shù):在自己內(nèi)部調(diào)用自己的函數(shù)名

3.重用函數(shù):require:用于靜態(tài)包含,include:用于動態(tài)包含,require_once:用于靜態(tài)包含,只包含一次,include_once:用于動態(tài)包含,只包含一次.

4.一些系統(tǒng)函數(shù)的使用:資源=opendir("目錄名"),readdir(資源)

php內(nèi)部函數(shù)代碼如下:

  1. function score($php,$java,$dotnet
  2.  { 
  3.   function php($php
  4.   { 
  5.    if($php>60) 
  6.     return "及格"
  7.    else 
  8.     return "不及格"
  9.   } 
  10.   function java($java
  11.   { 
  12.    if($java>60) 
  13.     return "及格"
  14.    else 
  15.     return "不及格"
  16.   } 
  17.   function dotnet($dotnet
  18.   { 
  19.    if($dotnet>60) 
  20.     return "及格"
  21.    else 
  22.     return "不及格"
  23.   } 
  24.    
  25.   $total=$php+$java+$dotnet
  26.   $agv=$total/3; 
  27.   echo "你的php成績是{$php}分,".php($php)."<br>"
  28.   echo "你的java成績是{$java}分,".java($java)."<br>"
  29.   echo "你的dotnet成績是{$dotnet}分,".dotnet($dotnet)."<br>"
  30.   echo "你的總分是:{$total}<br>"
  31.   echo "你的平均分是:{$agv}<br>"
  32.  } 
  33.   score(50,90,70); 
  34.  
  35.  //遞歸函數(shù) 
  36.  function demo($num
  37.  { 
  38.   echo $num."<br>"
  39.   if($num>0) 
  40.    demo($num-1); 
  41.   else 
  42.    echo "--------------------------------<br>"
  43.   echo $num."<br>"
  44.  } 
  45.  demo(10); 
  46.  
  47.  function total($dirname,&$dirnum,&$filename
  48.  { 
  49.   $dir=opendir($dirname); 
  50.   readdir($dir)."<br>"
  51.   readdir($dir)."<br>"
  52.   while($filename=readdir($dir)) 
  53.   { 
  54.    $newfile=$dirname."/".$filename
  55.    echo $filename."<br>"
  56.    if(is_dir($filename 
  57.   } 
  58.  
  59.  } 
  60.  $dirnum=0; 
  61.  $filenum=0; 
  62.  total("c:/windows",$dirnum,$filenum); 
  63.  echo "目錄總數(shù):".$dirnum."<br>"
  64.  echo "文件總數(shù):".$filenum."<br>"

構(gòu)造函數(shù)和析構(gòu)函數(shù)

構(gòu)造函數(shù):void __construct ([ mixed $args [, $... ]] )

PHP 5 允行開發(fā)者在一個類中定義一個方法作為構(gòu)造函數(shù),具有構(gòu)造函數(shù)的類會在每次創(chuàng)建對象時先調(diào)用此方法,所以非常適合在使用對象之前做一些初始化工作.

Note:如果子類中定義了構(gòu)造函數(shù)則不會暗中調(diào)用其父類的構(gòu)造函數(shù),要執(zhí)行父類的構(gòu)造函數(shù),需要在子類的構(gòu)造函數(shù)中調(diào)用 parent::__construct().

Example #1 使用新標(biāo)準(zhǔn)的構(gòu)造函數(shù),代碼如下:

  1. <?php 
  2. class BaseClass { 
  3.    function __construct() { 
  4.        print "In BaseClass constructorn"
  5.    } 
  6.  
  7. class SubClass extends BaseClass { 
  8.    function __construct() { 
  9.        parent::__construct(); 
  10.        print "In SubClass constructorn"
  11.    }//開源代碼phpfensi.com 
  12.  
  13. $obj = new BaseClass(); 
  14. $obj = new SubClass(); 
  15. ?> 

為了實現(xiàn)向后兼容性,如果 PHP 5 在類中找不到 __construct() 函數(shù),它就會嘗試尋找舊式的構(gòu)造函數(shù),也就是和類同名的函數(shù),因此唯一會產(chǎn)生兼容性問題的情況是:類中已有一個名為 __construct() 的方法,但它卻又不是構(gòu)造函數(shù).

析構(gòu)函數(shù):void __destruct ( void )

PHP 5 引入了析構(gòu)函數(shù)的概念,這類似于其它面向?qū)ο蟮恼Z言,如 C++,析構(gòu)函數(shù)會在到某個對象的所有引用都被刪除或者當(dāng)對象被顯式銷毀時執(zhí)行.

Example #2 析構(gòu)函數(shù)示例,代碼如下:

  1. <?php 
  2. class MyDestructableClass { 
  3.    function __construct() { 
  4.        print "In constructorn"
  5.        $this->name = "MyDestructableClass"
  6.    } 
  7.  
  8.    function __destruct() { 
  9.        print "Destroying " . $this->name . "n"
  10.    } 
  11.  
  12. $obj = new MyDestructableClass(); 
  13. ?> 

和構(gòu)造函數(shù)一樣,父類的析構(gòu)函數(shù)不會被引擎暗中調(diào)用,要執(zhí)行父類的析構(gòu)函數(shù),必須在子類的析構(gòu)函數(shù)體中顯式調(diào)用 parent::__destruct().

Note:析構(gòu)函數(shù)在腳本關(guān)閉時調(diào)用,此時所有的頭信息已經(jīng)發(fā)出.

Note:試圖在析構(gòu)函數(shù)中拋出一個異常會導(dǎo)致致命錯誤.

  1. <?php 
  2. class Foobar { 
  3.     public $baz
  4.     function __destruct() { 
  5.         # Don't do either of these, if $baz also has a __destruct()! 
  6.         $this->baz = null; 
  7.         unset($this->baz); 
  8.          
  9.         # Instead, don't clear it at all, or do this: 
  10.         $this->baz->__destruct(); 
  11.     } 
  12. ?> 
  13.  
  14. If you made this mistake, this might happen in php<5.3.6: 
  15. <?php 
  16. # Some function that throws an exception 
  17. function fail($foobar) { 
  18.     throw new Exception("Exception A!"); 
  19.  
  20. $foobar = new Foobar(); 
  21. $foobar->baz = new Foobar(); 
  22.  
  23. try { 
  24.     fail($foobar); // Send foobar to func that throws an Exception 
  25. } catch( Exception $e ) { 
  26.     print $e->getMessage(); // Exception A will be caught and printed, as expected. 
  27.  
  28. $foobar = null; // clearing foobar, and its property $baz 
  29.  
  30. try { 
  31.     print 'Exception B:';// this will be printed 
  32.     // output stops here. 
  33.     throw new Exception("Exception B!"); 
  34. } catch( Exception $e ) { 
  35.     print $e->getMessage(); // doesn't happen 
  36. print 'End'// this won't be printed 
  37. ?>
轉(zhuǎn)載請注明來源:php函數(shù)-系統(tǒng)函數(shù) 遞歸函數(shù) 重用函數(shù) 構(gòu)造函數(shù)使用

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。