Home  >  Article  >  Backend Development  >  Detailed explanation of PHP object-oriented philosophy

Detailed explanation of PHP object-oriented philosophy

小云云
小云云Original
2018-03-31 11:12:551179browse

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 &#39;<br >&#39;;

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(&#39;早上好&#39;,&#39;晚上好&#39;,&#39;你妹啊&#39;);        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 &#39;<br />&#39;; 

// 撞击$QQ->hit($lisi);echo $lisi->iq,&#39;<br >&#39;;$lisi->say();echo &#39;<br />&#39;; 

$lisi->say();echo &#39;<br />&#39;; 

$lisi->say();echo &#39;<br />&#39;;

           

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[&#39;gender&#39;];if($g == &#39;男&#39;) {    echo &#39;先生好&#39;;
} else if ($g == &#39;女&#39;) {    echo &#39;女士好&#39;;
} else {    echo &#39;妖猴!&#39;;
}echo &#39;<br >&#39;;

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(&#39;早上好&#39;,&#39;晚上好&#39;,&#39;你妹啊&#39;);        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 &#39;<br />&#39;; 

// 撞击$QQ->hit($lisi);echo $lisi->iq,&#39;<br >&#39;;$lisi->say();echo &#39;<br />&#39;; 

$lisi->say();echo &#39;<br />&#39;; 

$lisi->say();echo &#39;<br />&#39;;

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!

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