Home > Article > Backend Development > Detailed explanation of PHP object-oriented philosophy
This article mainly shares with you a detailed explanation of PHP's object-oriented philosophy. It mainly explains it to you in text, hoping to help you.
Before learning object-oriented, let’s review the process-oriented examples:
/*
Process-oriented
Receive the gender parameter male/female from the address bar
Simulate the behavior of welcoming guests in a shopping mall:
If the customer is a male: greet "Hello, sir"
If the customer is a female: greet "Hello, madam"
*/
$g = $_GET['gender'];if($g == '男') { echo '先生好'; } else if ($g == '女') { echo '女士好'; } else { echo '妖猴!'; }echo '<br >';
Simple object-oriented example:
/*
Unlucky Zhang San
Zhang San is an ordinary person. When others greet him in the morning, he will say "Good morning".
But one day he was hit by a car and suffered a little brain injury.
Therefore, when others greeted him in the morning,
he would sometimes say: "Good morning", but sometimes he would say: "In the evening Good" or even insult the other person.
*/
/*
Relevant elements we can see from this example:
张三
张三’s IQ
张Three hello
车
车杀人
*/
/*
Zhang San is an object
The car is also an object
张San has IQ—> Attribute [name]
Zhang San can say hello—> Function/method [verb]
Cars can hit people—> Function/method [verb]
*/
class Man { public $iq = 100; public function say() { $arr = array('早上好','晚上好','你妹啊'); if($this->iq >= 100) { echo $arr[0]; } else { $i = rand(0,2); echo $arr[$i]; } } }class Car { public function hit($people) { $newiq = rand(50,110); $people->iq = $newiq; } }$lisi = new Man();$QQ = new Car();$lisi->say();echo '<br />'; // 撞击$QQ->hit($lisi);echo $lisi->iq,'<br >';$lisi->say();echo '<br />'; $lisi->say();echo '<br />'; $lisi->say();echo '<br />';
Before learning object-oriented, let’s review the process-oriented examples:
/*
Process-oriented
From the address bar Receive gender parameter male/female
Simulate the behavior of welcoming guests in the mall:
If the customer is a male: greet "Hello, sir"
If the customer is a female: greet "Hello, madam"
*/
$g = $_GET['gender'];if($g == '男') { echo '先生好'; } else if ($g == '女') { echo '女士好'; } else { echo '妖猴!'; }echo '<br >';
Simple object-oriented example:
/*
Unlucky Zhang San
Zhang San is an ordinary person. Others morning Say hello to him and he will say "good morning".
But one day he was hit by a car and suffered a little brain injury.
Therefore, when others greeted him in the morning,
he would sometimes say: "Good morning", but sometimes he would say: "In the evening Good" or even insult the other person.
*/
/*
Relevant elements we can see from this example:
张三
张三’s IQ
张Three hello
车
车杀人
*/
/*
Zhang San is an object
The car is also an object
张San has IQ—> Attribute [name]
Zhang San can say hello—> Function/method [verb]
Cars can hit people—> Function/method [verb]
*/
class Man { public $iq = 100; public function say() { $arr = array('早上好','晚上好','你妹啊'); if($this->iq >= 100) { echo $arr[0]; } else { $i = rand(0,2); echo $arr[$i]; } } }class Car { public function hit($people) { $newiq = rand(50,110); $people->iq = $newiq; } }$lisi = new Man();$QQ = new Car();$lisi->say();echo '<br />'; // 撞击$QQ->hit($lisi);echo $lisi->iq,'<br >';$lisi->say();echo '<br />'; $lisi->say();echo '<br />'; $lisi->say();echo '<br />';
The above is the detailed content of Detailed explanation of PHP object-oriented philosophy. For more information, please follow other related articles on the PHP Chinese website!