| |
简单的理解,构造函数就是与类名称相同的方法,被称为构造函数。官方原话

构造函数是类中的一个特殊函数,当使用 new 操作符创建一个类的实例时,构造函数将会自动调用。当函数与类同名时,这个函数将成为构造函数。如果一个类没有构造函数,则调用基类的构造函数,如果有的话。

例如

<?php
  class mult{
   var $num = 1;
  
   function add($number){
     $this->num = $this->num + $number;
  }

  function mult($number = '10'){
    $this->num = $this->num * $number;
  }
}
?>

即当初始化mult时,如下 new mult(3) 时,自动给3乘以10
by admin | 来自 本站原创 | 不指定 2008-5-20 01:58 | 分类: PHP | 评论(0) | 引用(0) | 阅读(284)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]