1.內部函數:PHP可以在函數內部再聲明函數,目的就是在函數內部調用,用來幫助外部函數完成一些子功能.
2.遞歸函數:在自己內部調用自己的函數名
3.重用函數:require:用于靜態包含,include:用于動態包含,require_once:用于靜態包含,只包含一次,include_once:用于動態包含,只包含一次.
4.一些系統函數的使用:資源=opendir("目錄名"),readdir(資源)
php內部函數代碼如下:
- function score($php,$java,$dotnet)
- {
- function php($php)
- {
- if($php>60)
- return "及格";
- else
- return "不及格";
- }
- function java($java)
- {
- if($java>60)
- return "及格";
- else
- return "不及格";
- }
- function dotnet($dotnet)
- {
- if($dotnet>60)
- return "及格";
- else
- return "不及格";
- }
- $total=$php+$java+$dotnet;
- $agv=$total/3;
- echo "你的php成績是{$php}分,".php($php)."<br>";
- echo "你的java成績是{$java}分,".java($java)."<br>";
- echo "你的dotnet成績是{$dotnet}分,".dotnet($dotnet)."<br>";
- echo "你的總分是:{$total}<br>";
- echo "你的平均分是:{$agv}<br>";
- }
- score(50,90,70);
- //遞歸函數
- function demo($num)
- {
- echo $num."<br>";
- if($num>0)
- demo($num-1);
- else
- echo "--------------------------------<br>";
- echo $num."<br>";
- }
- demo(10);
- function total($dirname,&$dirnum,&$filename)
- {
- $dir=opendir($dirname);
- readdir($dir)."<br>";
- readdir($dir)."<br>";
- while($filename=readdir($dir))
- {
- $newfile=$dirname."/".$filename;
- echo $filename."<br>";
- if(is_dir($filename
- }
- }
- $dirnum=0;
- $filenum=0;
- total("c:/windows",$dirnum,$filenum);
- echo "目錄總數:".$dirnum."<br>";
- echo "文件總數:".$filenum."<br>";
構造函數和析構函數
構造函數:void __construct ([ mixed $args [, $... ]] )
PHP 5 允行開發者在一個類中定義一個方法作為構造函數,具有構造函數的類會在每次創建對象時先調用此方法,所以非常適合在使用對象之前做一些初始化工作.
Note:如果子類中定義了構造函數則不會暗中調用其父類的構造函數,要執行父類的構造函數,需要在子類的構造函數中調用 parent::__construct().
Example #1 使用新標準的構造函數,代碼如下:
- <?php
- class BaseClass {
- function __construct() {
- print "In BaseClass constructorn";
- }
- }
- class SubClass extends BaseClass {
- function __construct() {
- parent::__construct();
- print "In SubClass constructorn";
- }//開源代碼Vevb.com
- }
- $obj = new BaseClass();
- $obj = new SubClass();
- ?>
為了實現向后兼容性,如果 PHP 5 在類中找不到 __construct() 函數,它就會嘗試尋找舊式的構造函數,也就是和類同名的函數,因此唯一會產生兼容性問題的情況是:類中已有一個名為 __construct() 的方法,但它卻又不是構造函數.
析構函數:void __destruct ( void )
PHP 5 引入了析構函數的概念,這類似于其它面向對象的語言,如 C++,析構函數會在到某個對象的所有引用都被刪除或者當對象被顯式銷毀時執行.
Example #2 析構函數示例,代碼如下:
- <?php
- class MyDestructableClass {
- function __construct() {
- print "In constructorn";
- $this->name = "MyDestructableClass";
- }
- function __destruct() {
- print "Destroying " . $this->name . "n";
- }
- }
- $obj = new MyDestructableClass();
- ?>
和構造函數一樣,父類的析構函數不會被引擎暗中調用,要執行父類的析構函數,必須在子類的析構函數體中顯式調用 parent::__destruct().
Note:析構函數在腳本關閉時調用,此時所有的頭信息已經發出.
Note:試圖在析構函數中拋出一個異常會導致致命錯誤.
- <?php
- class Foobar {
- public $baz;
- function __destruct() {
- # Don't do either of these, if $baz also has a __destruct()!
- $this->baz = null;
- unset($this->baz);
- # Instead, don't clear it at all, or do this:
- $this->baz->__destruct();
- }
- }
- ?>
- If you made this mistake, this might happen in php<5.3.6:
- <?php
- # Some function that throws an exception
- function fail($foobar) {
- throw new Exception("Exception A!");
- }
- $foobar = new Foobar();
- $foobar->baz = new Foobar();
- try {
- fail($foobar); // Send foobar to func that throws an Exception
- } catch( Exception $e ) {
- print $e->getMessage(); // Exception A will be caught and printed, as expected.
- }
- $foobar = null; // clearing foobar, and its property $baz
- try {
- print 'Exception B:';// this will be printed
- // output stops here.
- throw new Exception("Exception B!");
- } catch( Exception $e ) {
- print $e->getMessage(); // doesn't happen
- }
- print 'End'; // this won't be printed
- ?>
新聞熱點
疑難解答