PHP Object-Oriented Programming example analysis, object-oriented programming
This paper analyzes the object-oriented programming method of PHP. Share to everyone for your reference, as follows:
In the development of ultra-large projects, if the use of process-oriented development, the amount of code is very large, which will be used in a lot of judgment and loop nesting, and many very similar code, not only make the project code volume is more large, not conducive to the development, reuse and maintenance.
Object-oriented can be a good solution to this problem, object-oriented very good encapsulation, save a lot of energy, do not care about the inner workings of objects, as long as the interaction between objects and objects, easy to maintain, and inheritance makes the code a lot of streamlining.
Let's look at a topic first:
Example: Zhang San is an ordinary person, others greet him in the morning, he will say "Good morning", but one day he was hit by a car, his brain was a little hurt, so when others greeted him in the morning, he sometimes said: "Morning", but sometimes said: "Nice evening", and even insult each other.
Analysis: Because now we're going to object-oriented, let's get the object out first.
Object 1: Zhang San (person) IQ (name is attribute) greeting (verb is method)
Object 2: The car hits people (Verbs are methods)
<?php//Create a human class, a class is an instance of an object (the mold in the factory) class human{ //Average person's IQ is public $iq =; Hello, morning see people Good morning public function greet () { echo ' Good Morning '; }} Instantiate an object John Doe $lisi = new Human (); $lisi->greet ();//Return to Good morning?>
This is an instantiation of a normal John Doe
Look at the John Doe that got hit by a car.
<?php//Create a human class, a class is an instance of an object (the mold in the factory) class human{ //Average person's IQ is public $iq =; Hello, morning see people Good morning public function greet () { //When IQ is greater than or equal to ($this->iq>=100) { echo ' Good Morning ', '
'; } else{ //When IQ is less than 100, the following greeting appears randomly $regard = array (' Good morning ', ' evening ', ' bastard '); echo $regard [rand (0,2)], '
'; } }} Instantiate an object John Doe $lisi = new Human (); $lisi->greet ();//Return to Good Morning class car{public function hit ($people) { // After the crash, People's IQ randomly changed $people->iq=rand (40,120);} } Instantiate a BMW car $baoma = new car ();//BMW hit the $baoma->hit ($lisi);//The IQ of the person after the crash $lisi->iq, '
';//people according to the IQ of how much, greetings are not the same $lisi->greet ();? >
More readers interested in object-oriented content in PHP can view the topic: "PHP Object-oriented Programming primer tutorial"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- Examples of constant usages such as PHP object-oriented programming
- Interface usage of PHP object-oriented programming
- PHP Object-oriented programming (OOP) learning notes (v)-PHP namespaces
- PHP Object-oriented programming (OOP) learning notes (iv)-Exception handling class exception
- PHP Object-oriented programming (OOP) learning Notes (iii)-singleton mode and Factory mode
- PHP Object-oriented programming (OOP) learning Notes (ii)-Properties and methods of static variables and delay binding
- PHP Object-oriented programming (OOP) learning notes (i)-abstract classes, object interfaces, instanceof, and contract programming
- PHP Learning notes PHP Object-oriented programming
- PHP5 Object-Oriented programming
- PHP in-depth understanding of object-oriented programming overloading and Method overlay (polymorphic)
- PHP Object-oriented description (III)
http://www.bkjia.com/PHPjc/1096146.html www.bkjia.com true http://www.bkjia.com/PHPjc/1096146.html techarticle PHP Object-Oriented Programming example analysis, object-oriented programming this paper analyzes the PHP object-oriented programming method. Share to everyone for your reference, as follows: In the super ...