Home Backend Development PHP Tutorial Avoid OOP forms, POP ideas_PHP tutorial

Avoid OOP forms, POP ideas_PHP tutorial

Jul 13, 2016 pm 05:25 PM
oop pop exist Thought article of avoid

Avoid the form of OOP, the idea of ​​​​POP
I haven’t posted a technical article for a long time. Today I saw a brother posted this post in 21PHP and forwarded it. It is useful for strengthening the learning of OOP
--- -------------------------------------------------- -----
Avoid the form of OOP, the idea of ​​​​POP
With the enhancement of PHP's support for OOP (object-oriented programming) and the development of programming ideas, more and more PHP programmers have entered The world of OOP. Through simple learning, most programmers can
quickly master the basic skills of OOP programming. There are more and more strings such as class and new in the code written by programmers. But as a program As a programmer, have you fallen into a programming trap of OO form and PO thinking?
As programmers in the POP era, in order to calculate the price of all the doors of a house, they will write such a function .
Code:
/******************************************
Function: priceOfHouseDoor($num, $price)
Function: Calculate the sum of the prices of all doors
Parameters: $num the number of doors, $price the price of each door
Remarks: surfchen @ http://www.yubeinet.com/
***********************************************/
function priceHouseDoor($num,$price)
{
return $num*$price;
}
echo priceHouseDoor(2,5);//Output price
or simply update the process:
Code:
$doors=2;//Number of doors
$price=5;// Price of each door
echo $doors*$price;//Output price
This is a typical POP. After understanding OOP, many people may change the code that implements this function. Perhaps, programmers It would be written like this:
Code:
class house
{
/******************************************

Function: getDoorPrice( $num,$price)
Function: Calculate the sum of the prices of all doors
Parameters: $num number of doors, $price price of each door
Remarks: surfchen @ http://www.yubeinet .com/
******************************************/
function getDoorPrice($num,$price)
{
return $ num*$price;
}
}
$house=new house;
echo $house->getDoorPrice(2,5);//Output price
We can see it at once , this code uses the representatives of OOP - classes and instantiation. So, is this object-oriented programming?
I took a closer look and found that this class is actually just the priceOfHouseDoor($num,$ price) function.
Well, now let’s discuss what object-oriented programming is. My understanding is: object-oriented programming has two core contents. One is classes, and the other is instances. Classes are An abstraction of a class of things, and instances
are individuals in this class of things. The attributes (values) and methods (functions) of instances are the specific content and actions of this individual respectively.
We Let’s take a look at the house class above. It has a method, getDoorPrice($num,$price), which introduces two parameters. One is the number of doors in the house, and the other is the door price of the house. We
found that when When we use this method, these two values ​​​​are constructed temporarily and do not form any connection with the house. In real life, the door in a house has no connection with the house, which is incomprehensible. ..As we know, OOP is actually a microcosm of our real world. Therefore, the house in OOP has no connection with the door of the house, and it is also incomprehensible. The thinking of right and wrong OO
.
Then , how should we use real OO to achieve the above functions? Please see the following code:
Code:
class house
{
var $doors;//Number of doors
var $ pricePerDoors;//The price of each door
function setNumOfDoors($num){$this->doors=$num;}//Set the number of doors
function setPricePerDoor($price){$this->pricePerDoors =$price;}//Set the price of each door
/*************
*Get the price of the door*
*************/
function getDoorPrice()
{
return ($this->doors)*($ this->pricePerDoors);
}
}
$house=new house;
$house->setNumOfDoors(2);//Set the number of doors
$house->setPricePerDoor( 5);//Set the price of each door
echo $house->getDoorPrice();//Output
We can clearly see that the number of doors and the price of the house in the above code have been combined Together. This is the real object-oriented.
Don’t you think? ^_^


http://www.bkjia.com/PHPjc/532013.html

truehttp: //www.bkjia.com/PHPjc/532013.htmlTechArticleAvoid OOP form, POP thinking. I haven’t posted a technical article for a long time. Today I saw a brother in 21PHP I posted this post and retweeted it. It is useful for strengthening OOP learning----------------------...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) Dec 31, 2023 pm 05:15 PM

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! Mar 15, 2024 pm 04:13 PM

1. How can you make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! 1. Activate basic rights and interests: original articles can earn profits by advertising, and videos must be original in horizontal screen mode to earn profits. 2. Activate the rights of 100 fans: if the number of fans reaches 100 fans or above, you can get profits from micro headlines, original Q&A creation and Q&A. 3. Insist on original works: Original works include articles, micro headlines, questions, etc., and are required to be more than 300 words. Please note that if illegally plagiarized works are published as original works, credit points will be deducted, and even any profits will be deducted. 4. Verticality: When writing articles in professional fields, you cannot write articles across fields at will. You will not get appropriate recommendations, you will not be able to achieve the professionalism and refinement of your work, and it will be difficult to attract fans and readers. 5. Activity: high activity,

The first choice for CS players: recommended computer configuration The first choice for CS players: recommended computer configuration Jan 02, 2024 pm 04:26 PM

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Java Error: JavaFX View Error, How to Handle and Avoid Java Error: JavaFX View Error, How to Handle and Avoid Jun 25, 2023 am 08:47 AM

JavaFX is a user interface framework for the Java platform, similar to Swing, but more modern and flexible. However, you may encounter some view errors when using it. This article will introduce how to deal with and avoid these errors. 1. Types of JavaFX view errors When using JavaFX, you may encounter the following view errors: NullPointerException This is one of the most common errors and usually occurs when trying to access an uninitialized or non-existent object. This may

Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Jan 01, 2024 pm 12:52 PM

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

Java Error: Encoding and Decoding Errors, How to Solve and Avoid Java Error: Encoding and Decoding Errors, How to Solve and Avoid Jun 24, 2023 pm 05:27 PM

Java is a very popular programming language and many projects are written in Java. However, when we encounter "Encoding and Decoding Errors" during the development process, we may feel confused and confused. In this article, we will introduce the causes of Java encoding and decoding errors, how to solve and avoid these errors. What is a codec error? During Java development, we often need to process text and files. However, different texts and files may make

Java Errors: JDBC Errors, How to Solve and Avoid Java Errors: JDBC Errors, How to Solve and Avoid Jun 24, 2023 pm 02:40 PM

With the widespread application of Java, JDBC errors often occur when Java programs connect to databases. JDBC (JavaDatabaseConnectivity) is a programming interface in Java used to connect to a database. Therefore, a JDBC error is an error encountered when a Java program interacts with a database. Here are some of the most common JDBC errors and how to solve and avoid them. ClassNotFoundException This is the most common JDBC

See all articles