This paper analyzes the Object-oriented programming method of PHP. Share to everyone for your reference, specific as follows:
During the development of super large projects, if the use of process-oriented development, the amount of code is very large, which will use a lot of judgment and loop nesting, and many very similar code, not only to make the project code volume is larger, but also not conducive to 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 internal operation of objects, as long as the concern between object and object interaction, easy to maintain, and inheritance makes the code a large number of streamlining.
Let's look at a topic first:
John is an ordinary person who greets him in the morning, he would say "Good morning", but one day he got hit by a car and had a little brain injury, so when someone greets him in the morning, he sometimes says "morning", but sometimes he says, "Nice evening," and even insults each other.
Analysis: Because now we're going to object-oriented, let's get the object out first.
Object 1: John (person) IQ (name is attribute) Hello (verb is method)
Object 2: Car hit people (verb is method)
<?php
//Create a human class, the class is an instance of the object (mold in the factory) class
human{
//Average IQ is public
$iq =;
Hello, good morning to see people. Public
function greet () {
echo ' Good Morning ';
}
}
Instantiate an object dick
$lisi = new Human ();
$lisi->greet ();
Back Good morning
?>
This is the instantiation of a normal dick
Here's a look at the dick that got hit by a car.
<?php
//Create a human class, the class is an instance of the object (mold in the factory) class
human{
//Average IQ is public
$iq =;
Hello, good morning to see people. Public
function greet () {
//When IQ is greater than or equal to
($this->iq>=100) {
echo ' Good Morning ', ' < br/> ';
} else{
//When IQ is less than 100, the following greeting is randomly
$regard = array (' Good morning ', ' evening ', ' jerk ');
echo $regard [rand (0,2)], ' <br/> ';
}}} Instantiate an object dick
$lisi = new Human ();
$lisi->greet ();
Return to Good morning
class car{public
function hit ($people) {
//The IQ of a person has changed randomly
$people->iq=rand (40,120 );
}
}
Instantiate a BMW
$baoma = new Car ();
BMW car hit a man
$baoma->hit ($lisi);
After hitting people's IQ
echo $lisi->iq, ' <br/> ';
According to the IQ of the number of people, greetings are not the same
$lisi->greet ();
>
More about PHP-oriented content readers can view the site topics: "PHP Object-oriented Program design introductory Course"
I hope this article will help you with the PHP program design.