實例化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();//田妞要吃飯
?>
新聞熱點
疑難解答