_php example based on the Object-oriented member method of PHP

Source: Internet
Author: User

Read the member method with one of the following: You can write it yourself and deepen your understanding.

such a demand ; I hope that people can talk, do arithmetic problems ..., so you need to use the member method:
1, add Speak member method, output I am xiaoming
2, add Jisuan member method, can calculate from 1+. +1000 of the results
3, modifies the Jisuan member method, this method may receive a number N, calculates 1+. The result of +n
4, add member method, you can calculate the number of two and

Reference code:

Copy Code code as follows:

<?php

Class person{

Public $name;
Public $age;

Add Member Method
Public Function speak () {

echo "I am xiaoming";
}

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

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

Calculate the number of 2 and
Public function Add ($num 1, $num 2) {
return $num 1+ $num 2;
}
}

$person 1=new person;
People talking
$person 1->speak (). ' <br/> ';
Calculation
Echo ' <br/> ' $person 1->jisuan ();
Echo ' <br/> ' $person 1->jisuan (100);
With parameters of
Echo ' Calculates the result: '. $person 1->jisuan2 (5);
Calculate the number of 2 and
echo "<br/>50+50=". $person 1->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.