Home > php教程 > php手册 > 实例化php类的时候如何传参

实例化php类的时候如何传参

WBOY
Release: 2016-06-06 19:49:01
Original
2004 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 当我们实例化一个php类的时候,要怎么传递参数呢?这取决于该类的构造方法。 例: person.class.php ?php class person{ var $name; var $color; var $sex; var $age; function __construct($name,$

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

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

  例:

  person.class.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

  

  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类的时候如何传参

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template