PHP object-oriented member methods-php instances

Source: Internet
Author: User
This article describes the PHP object-oriented member methods. If you need a friend, refer to the following method to use a column to read members: you can write a column by yourself to deepen your understanding.

Such a requirementHope that people can talk and do arithmetic..., so you need to use the member method:
1. Add the speak member method. The output is James.
2. Add the jisuan member method to calculate the result from 1 +... + 1000
3. Modify the jisuan member method. This method can receive a number n and calculate the result of 1 +... + n.
4. add a member method to calculate the sum of two numbers

Reference code:

The Code is as follows:


Class Person {

Public $ name;
Public $ age;

// Add member Method
Public function speak (){

Echo "I am James ";
}

Public function jisuan (){
// Calculate the result from 1 +... + 1000
$ Result = 0;
For ($ I = 1; $ I <= 1000; $ I ++ ){
$ Result + = $ I;
}
// Returns the calculated result ruturn.
Return $ result;
}

Public function jisuan2 ($ n ){
// Calculate the result from 1 +... + n
$ Result = 0;
For ($ I = 1; $ I <= $ n; $ I ++ ){
$ Result + = $ I;
}
// Return
Return $ result;
}

// Calculate the sum of 2 numbers
Public function add ($ num1, $ num2 ){
Return $ num1 + $ num2;
}
}

$ Person1 = new Person;
// Speak
$ Person1-> speak ().'
';
// Computing
Echo'
'. $ Person1-> jisuan ();
// Echo'
'. $ Person1-> jisuan (100 );
// With Parameters
Echo: '. $ person1-> jisuan2 (5 );
// Calculate the sum of 2 numbers
Echo"
50 + 50 = ". $ person1-> add (50, 51 );
?>

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.