Home  >  Article  >  Backend Development  >  Detailed explanation of object-oriented member methods based on PHP_PHP tutorial

Detailed explanation of object-oriented member methods based on PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:41874browse

Use an example to understand the member method: you can write it yourself to deepen your understanding.

Such a requirement; I hope people can speak and do arithmetic problems..., so you need to use member methods:
1. Add the speak member method and output I am Xiao Ming
2. Add the jisuan member method, which can calculate the result from 1+..+1000
3. Modify the jisuan member method, which can receive a number n and calculate the result of 1+..+n
4. Add the add member method to calculate the sum of two numbers

Reference code:

Copy code The code is as follows:

class Person{

public $name;
public $age;

//Add member method
public function speak(){

echo "I am Xiao Ming";
}

public function jisuan(){
/ /Calculate the result from 1+..+1000
                                $result =0;                        >}
// Return the calculation result ruturn back
Return $ Result;
}

public function jisuan2($n){
//Calculate the result from 1+..+n

;$i++){

                         $result+=$i;                                    
                                                                                                                                                                                                                                              
$person1=new Person;
//Person speaks
$person1->speak().'
';
//Calculation
echo '< br/>'.$person1->jisuan();
//echo '
'.$person1->jisuan(100);
//With parameters echo 'The calculated result is:'.$person1->jisuan2(5);

//Calculate the sum of 2 numbers
echo "
50+50=".$person1 ->add(50,51);
?>





http://www.bkjia.com/PHPjc/327047.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/327047.html

TechArticle
Use an example to understand the member method: you can write it yourself to deepen your understanding. Such a demand; I hope people can speak and do arithmetic problems..., so you need to use it...

Statement:
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