当前位置:首页 > PHP教程 > php函数 > 列表

php函数-系统函数 递归函数 重用函数 构造函数使用

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-18 16:48:53 浏览: 评论:0 

1.内部函数:PHP可以在函数内部再声明函数,目的就是在函数内部调用,用来帮助外部函数完成一些子功能.

2.递归函数:在自己内部调用自己的函数名

3.重用函数:require:用于静态包含,include:用于动态包含,require_once:用于静态包含,只包含一次,include_once:用于动态包含,只包含一次.

4.一些系统函数的使用:资源=opendir("目录名"),readdir(资源)

php内部函数代码如下:

  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.  //递归函数 
  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 "目录总数:".$dirnum."<br>"
  64.  echo "文件总数:".$filenum."<br>"

构造函数和析构函数

构造函数:void __construct ([ mixed $args [, $... ]] )

PHP 5 允行开发者在一个类中定义一个方法作为构造函数,具有构造函数的类会在每次创建对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作.

Note:如果子类中定义了构造函数则不会暗中调用其父类的构造函数,要执行父类的构造函数,需要在子类的构造函数中调用 parent::__construct().

Example #1 使用新标准的构造函数,代码如下:

  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. ?> 

为了实现向后兼容性,如果 PHP 5 在类中找不到 __construct() 函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数,因此唯一会产生兼容性问题的情况是:类中已有一个名为 __construct() 的方法,但它却又不是构造函数.

析构函数:void __destruct ( void )

PHP 5 引入了析构函数的概念,这类似于其它面向对象的语言,如 C++,析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行.

Example #2 析构函数示例,代码如下:

  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. ?> 

和构造函数一样,父类的析构函数不会被引擎暗中调用,要执行父类的析构函数,必须在子类的析构函数体中显式调用 parent::__destruct().

Note:析构函数在脚本关闭时调用,此时所有的头信息已经发出.

Note:试图在析构函数中抛出一个异常会导致致命错误.

  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. ?>

Tags: php系统函数 php构造函数

分享到:

相关文章