PHP object-oriented programming example analysis, object-oriented programming _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming example analysis, object-oriented programming. PHP object-oriented programming example analysis, object-oriented programming this article examples analysis PHP object-oriented programming method. I will share with you for your reference. The details are as follows: in the analysis of super PHP object-oriented programming examples, object-oriented programming

This article analyzes the PHP object-oriented programming method. We will share this with you for your reference. The details are as follows:

In the development process of super-large projects, if process-oriented development is used, the amount of code is very large, which will use a lot of judgment and loop nesting, and a lot of very similar code, it not only makes the project code a huge amount, but also is not conducive to development, reuse and maintenance.

Object-oriented can solve this problem very well. object-oriented encapsulation saves a lot of energy and does not need to care about the internal operation of objects. as long as you care about the interaction between objects, it is easy to maintain, and inheritance makes the code much simpler.

Let's first look at a question:

For example, Michael is an ordinary person. When someone greets him in the morning, he will say "good morning". But one day he was hit by a car and his mind was hurt, when others say hello to him in the morning, he sometimes says "good morning", but sometimes says "good evening" or even insulted the other party.

Analysis: Because object orientation is required now, we should take out the object first.
Object 1: Zhang San (person) IQ (name is attribute) greeting (verb is method)
Object 2: car hitting (verb is a method)

<? Php // create a human class. the class is an object instance (a model in the factory) class human {// The iq of ordinary people is 100 public $ iq = 100; // greeting, good morning to see people. public function greet () {echo 'Good Morning ';} // instantiate an object Li Si $ lisi = new human (); $ lisi-> greet (); // returns good morning?>

This is a normal example of Li Si.

Let's take a look at the 4 Li that was hit by a car.

<? Php // create a human class. the class is an object instance (a model in the factory) class human {// The iq of ordinary people is 100 public $ iq = 100; // greeting, good morning to see people. public function greet () {// when the iq is greater than or equal to 100 if ($ this-> iq> = 100) {echo 'Good Morning ','
';} Else {// when the IQ is less than 100, the following greetings $ regard = array ('Good Morning', 'Good evening ', 'Asshole') are randomly displayed '); echo $ regard [rand (0, 2)],'
';}}// Instantiate an object Li Si $ lisi = new human (); $ lisi-> greet (); // return good morning class car {public function hit ($ people) {// after hitting a person's iq, a random change occurs $ people-> iq = rand (40,120 );}} // instantiate a BMW car $ baoma = new car (); // The BMW car hits $ baoma-> hit ($ lisi ); // human iq echo $ lisi-> iq ,'
'; // The greetings vary according to the IQ of a person. $ lisi-> greet ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.