建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > PHP学习 > php系统函数 递归函数 重用函数 构造函数介绍

php系统函数 递归函数 重用函数 构造函数介绍

时间:2015-05-17 21:17:40  来源:免费模板网 作者:风雪 阅读次数 tags递归重用构造

本文讲解系统函数 递归函数 重用函数 构造函数的介绍使用
/*
*  1.内部函数:PHP可以在函数内部再声明函数
*    目的就是在函数内部调用
*    用来帮助外部函数完成一些子功能
*
*  2.递归函数:在自己内部调用自己的函数名
*
*  3.重用函数
*
*   require:用于静态包含
*   include:用于动态包含
*   require_once:用于静态包含,只包含一次
*   include_once:用于动态包含,只包含一次
*
*  4.一些系统函数的使用
* 资源=opendir("目录名")
* readdir(资源)
*
*
*/
//内部函数
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)."
";
  echo "你的java成绩是{$java}分,".java($java)."
";
  echo "你的dotnet成绩是{$dotnet}分,".dotnet($dotnet)."
";
  echo "你的总分是:{$total}
";
  echo "你的平均分是:{$agv}
";
}
  score(50,90,70);

//递归函数
function demo($num)
{
  echo $num."
";
  if($num>0)
   demo($num-1);
  else
   echo "--------------------------------
";
  echo $num."
";
}
demo(10);

function total($dirname,&$dirnum,&$filename)
{
  $dir=opendir($dirname);
  readdir($dir)."
";
  readdir($dir)."
";
  while($filename=readdir($dir))
  {
   $newfile=$dirname."/".$filename;
   echo $filename."
";
   if(is_dir($filename
  }
}
$dirnum=0;
$filenum=0;
total("c:/windows",$dirnum,$filenum);
echo "目录总数:".$dirnum."
";
echo "文件总数:".$filenum."
";
?>
构造函数和析构函数
构造函数
void __construct ([ mixed $args [, $... ]] )
PHP 5 允行开发者在一个类中定义一个方法作为构造函数。具有构造函数的类会在每次创建对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作。
Note: 如果子类中定义了构造函数则不会暗中调用其父类的构造函数。要执行父类的构造函数,需要在子类的构造函数中调用 parent::__construct()。
Example #1 使用新标准的构造函数
class BaseClass {
   function __construct() {
       print "In BaseClass constructorn";
   }
}
class SubClass extends BaseClass {
   function __construct() {
       parent::__construct();
       print "In SubClass constructorn";
   }
}
$obj = new BaseClass();
$obj = new SubClass();
?>

为了实现向后兼容性,如果 PHP 5 在类中找不到 __construct() 函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数。因此唯一会产生兼容性问题的情况是:类中已有一个名为 __construct() 的方法,但它却又不是构造函数。
析构函数
void __destruct ( void )
PHP 5 引入了析构函数的概念,这类似于其它面向对象的语言,如 C++。析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。
Example #2 析构函数示例
class MyDestructableClass {
   function __construct() {
       print "In constructorn";
       $this->name = "MyDestructableClass";
   }
   function __destruct() {
       print "Destroying " . $this->name . "n";
   }
}
$obj = new MyDestructableClass();
?>

和构造函数一样,父类的析构函数不会被引擎暗中调用。要执行父类的析构函数,必须在子类的析构函数体中显式调用 parent::__destruct()。
Note:
析构函数在脚本关闭时调用,此时所有的头信息已经发出。

Note:
试图在析构函数中抛出一个异常会导致致命错误。
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:
# 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
?>

本文地址:https://www.freemoban.com/php/2015/0517/1856.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!