A5下载 - 努力做内容最丰富最安全的下载站!

A5站长下载站

当前位置:A5下载 > 源码技巧 > 父类数据 > 实例化php类如何传参

实例化php类如何传参

时间:2015-05-29 09:51作者:sa人气:328

实例化php类的时候如何传参???当我们实例化一个php类的时候,要怎么传递参数呢?这取决于该类的构造方法。

例:

person.class.php

<?php

class person{

var $name;

var $color;

var $sex;

var $age;

function __construct($name,$age='',$sex='boy'){

$this->name = $name;

$this->age = $age;

$this->sex = $sex;

$this->color = 'yello';

}

function eat(){

echo $this->name.'要吃饭';

}

function xinxi(){

echo $this->name.' is '.$this->sex.' and age is '.$this->age.' fuse is '.$this->color;

}

}

?>

son.php

<?php

include('person.class.php');

$son = new person('cuihua',25,'girl');//此处的参数传递要和类的构造方法里面的参数顺序对应

//$son->xinxi();//cuihua is girl and age is 25 fuse is yello
$son->name = '田妞';

$son->eat();//田妞要吃饭

?>

 

标签实例,php,如何,传参,实例,php,时候,如何,传参,我

相关下载

查看所有评论+

网友评论

网友
您的评论需要经过审核才能显示

公众号