How to pass parameters when instantiating a php class

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. How do we pass parameters when instantiating a php class? This depends on the constructor of the class. Example: person. class. php? Phpclassperson {var $ name; var $ color; var $ sex; var $ age; function _ construct ($ name, $

Welcome to the Linux community forum and interact with 2 million technicians> How do we pass parameters when instantiating a php class? This depends on the constructor of the class. Example: person. class. php? Php class person {var $ name; var $ color; var $ sex; var $ age; function _ construct ($ name, $

Welcome to the Linux community forum and interact with 2 million technicians>

How do we pass parameters when instantiating a php class? This depends on the constructor of the class.

Example:

Person. class. php

  

Class person {

Var $ name;

Var $ color;

Var $ sex;

Var $ age;

Function _ construct ($ name, $ age = '', $ sex = 'boys '){

$ This-> name = $ name;

$ This-> age = $ age;

$ This-> sex = $ sex;

$ This-> color = 'yello ';

}

Function eat (){

Echo $ this-> name. 'Have dinner ';

}

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 '); // The parameter transfer here corresponds to the parameter order in the class constructor.

// $ Son-> xinxi (); // cuihua is girl and age is 25 fuse is yello

$ Son-> name = 'genniu ';

$ Son-> eat (); // Tian Niu wants to eat

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.