A detailed _php instance based on PHP object-oriented member method

Source: Internet
Author: User
Use an alternative to read the Member method: You can write your own writing, deepen understanding.

such a demand ; Hope that people can speak, do arithmetic problems ..., so you need to use the member method:
1. Add Speak member method, output I'm xiaoming.
2, add Jisuan member method, you can calculate from 1+. Results of +1000
3, modify the Jisuan member method, the method can receive a number n, calculate 1+. Results of +n
4. Adding the Add Member method, you can calculate two number of and

Reference code:
Copy the Code code as follows:

Class person{

Public $name;
Public $age;

Add Member Method
Public Function speak () {

echo "I am xiaoming";
}

Public Function Jisuan () {
Calculated from 1+. Results of +1000
$result = 0;
for ($i =1; $i <=1000; $i + +) {
$result + = $i;
}
Return the result of the calculation Ruturn
return $result;
}

Public Function jisuan2 ($n) {
Calculated from 1+. Results of +n
$result = 0;
for ($i =1; $i <= $n; $i + +) {
$result + = $i;
}
Return
return $result;
}

Calculates the 2-digit and
Public function Add ($num 1, $num 2) {
return $num 1+ $num 2;
}
}

$person 1=new person;
People talking
$person 1->speak (). '
';
Calculation
Echo '
'. $person 1->jisuan ();
Echo '
'. $person 1->jisuan (100);
With the parameter
The result of Echo ' calculation is: '. $person 1->jisuan2 (5);
Calculates the 2-digit and
echo "
50+50= ". $person 1->add (50,51);
?>

  • Related Article

    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.