Home >Backend Development >PHP Tutorial >Detailed explanation of object-oriented member methods based on PHP_PHP tutorial
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:
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
$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