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: 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

技嘉的主板怎么设置键盘开机首先,要支持键盘开机,一定是PS2键盘!!设置步骤如下:第一步:开机按Del或者F2进入bios,到bios的Advanced(高级)模式普通主板默认进入主板的EZ(简易)模式,需要按F7切换到高级模式,ROG系列主板默认进入bios的高级模式(我们用简体中文来示范)第二步:选择到——【高级】——【高级电源管理(APM)】第三步:找到选项【由PS2键盘唤醒】第四步:这个选项默认是Disabled(关闭)的,下拉之后可以看到三种不同的设置选择,分别是按【空格键】开机、按组

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

JavaFX是Java平台的一个用户界面框架,类似于Swing,但却更加现代化和灵活。然而在使用时可能会遇到一些视图错误,本文将介绍如何处理和避免这些错误。一、JavaFX视图错误的类型在使用JavaFX时,可能会遇到以下几种视图错误:NullPointerException这是最常见的错误之一,通常在尝试访问未初始化或不存在的对象时发生。这可能

广联达软件是一家专注于建筑信息化领域的软件公司,其产品被广泛应用于建筑设计、施工、运营等各个环节。由于广联达软件功能复杂、数据量大,对电脑的配置要求较高。本文将从多个方面详细阐述广联达软件的电脑配置推荐,以帮助读者选择适合的电脑配置处理器广联达软件在进行建筑设计、模拟等操作时,需要进行大量的数据计算和处理,因此对处理器的要求较高。推荐选择多核心、高主频的处理器,如英特尔i7系列或AMDRyzen系列。这些处理器具有较强的计算能力和多线程处理能力,能够更好地满足广联达软件的需求。内存内存是影响计算

Java是一门非常流行的编程语言,许多项目都是由Java编写的。然而,当我们在开发过程中遇到“编解码错误”(EncodingandDecodingErrors)时,可能会感到困惑和疑惑。在本文中,我们将介绍Java编解码错误的原因、如何解决和避免这些错误。什么是编解码错误?在Java开发过程中,我们经常需要处理文本和文件。然而,不同的文本和文件可能使

随着Java的广泛应用,Java程序在连接数据库时经常会出现JDBC错误。JDBC(JavaDatabaseConnectivity)是Java中用于连接数据库的编程接口,因此,JDBC错误是在Java程序与数据库交互时遇到的一种错误。下面将介绍一些最常见的JDBC错误及如何解决和避免它们。ClassNotFoundException这是最常见的JDBC

在PHP语言开发中,经常会遇到无限循环的情况,它会无限制地执行某些代码,导致程序崩溃甚至服务器崩溃。本文将介绍一些避免陷入无限循环的方法,帮助开发人员更好地解决这一问题。1.避免在循环中进行无限递归调用当在循环中调用一个函数或方法时,如果函数或方法中又包含了循环语句,就会形成无限递归调用,导致程序崩溃。为避免这种情况的发生,可以在递归调用函数或方法时,添加一

电脑100兆网口怎么变成千兆的要将电脑的100兆网口升级为千兆网口,一般需要按照以下步骤进行操作:1.确认网卡是否支持千兆网速:首先需要确认电脑上的网卡是否支持千兆以太网,如果不支持的话就无法实现将100兆网口提速至千兆。2.更换网线:要实现千兆网速,需要使用Cat5e或更高规格的网线,因为Cat5e网线可以支持千兆以太网,而对于100兆以太网来说则只需使用Cat5网线即可。3.更改网卡驱动程序:如果您的网卡支持千兆以太网,则需要更新网卡的驱动程序。可以前往网卡制造商的官方网站,下载并安装最新的


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor
