通用方法:(使用参数的默认值实现)
<?php
class myclass
{
var $name;
var $age;
function myclass($name_="凌", $age_=23)
{
$this -> name = $name_;
$this -> age = $age_;
}
function showinfo($obj)
{
echo "<div>\$".$obj." -> name = ". $this -> name.";\$".$obj." -> age=".$this -> age."</div>" ;
}
}
$myfun = new myclass();
$myfun -> showinfo("myfun");
$myfun1 = new myclass("张三");
$myfun1 -> showinfo("myfun1");
$myfun2 = new myclass("李四",25);
$myfun2 -> showinfo("myfun2");
?>
另一种方式(使用系统函数实现)
<?php
//函数重载
class myfun
{
var $name;
var $age;
function myfun1($name_)
{
$this -> name = $name_;
$this -> age = 23;
}
function myfun2($name_, $age_)
{
$this -> name = $name_;
$this -> age = $age_;
}
function myfun()
{
$args_num = func_num_args(); //获取参数个数
$args = func_get_args(); //获取参数列表
switch($args_num)
{
case 1:
$this -> myfun1($args[0]);
break;
case 2:
$this -> myfun2($args[0], $args[1]);
break;
default:
$this -> name = "凌";
$this -> age = 23;
break;
}
}
function showinfo($obj)
{
echo "<div>\$".$obj." -> name = ". $this -> name.";\$".$obj." -> age=".$this -> age."</div>" ;
}
}
$myfun = new myfun();
$myfun -> showinfo("myfun");
$myfun1 = new myfun("张三");
$myfun1 -> showinfo("myfun1");
$myfun2 = new myfun("李四",25);
$myfun2 -> showinfo("myfun2");
?>
posted on 2006-04-24 08:52
蕃豆仁 阅读(3617)
评论(13) 编辑 收藏 引用 所属分类:
PHP自学之路