Home php教程 php手册 PHP中的面向对象和面向过程的编程方法

PHP中的面向对象和面向过程的编程方法

Jun 13, 2016 am 10:32 AM
php and genius object method have correct of Introduction programming evaluate process For risk


  简介
  “真正的天才具有正确评价不确定的,有风险的和矛盾的信息的能力。--邱吉尔”
  
  使用许多编程语言时,你通常只能使用面向对象或面向过程二者之一的编程方式。而在php(做为现在的主流开发语言)中,你可以自由选择或混用。目前绝大多数php(做为现在的主流开发语言)程序员使用面向过程的方式,因为解析WEB页面本身就非常“过程化”(从一个标签到另一个标签)。在HTML中嵌入过程处理代码是很直接自然的作法,所以php(做为现在的主流开发语言)程序员通常使用这种方式。
  
  如果你是刚接触php(做为现在的主流开发语言),用面向过程的风格来书写代码很可能是你唯一的选择。但是如果你经常上php(做为现在的主流开发语言)论坛和新闻组的话,你应该会看到有关“对象”的文章。你也可能看到过如何书写面向对象的php(做为现在的主流开发语言)代码的教程。或者你也可能下载过一些现成的类库,并尝试着去实例化其中的对象和使用类方法--尽管你可能没有真正理解这些类为什么可以工作,或者为什么需要使用面向对象的方法来实现功能。
  
  应该使用“面向对象”的风格还是“面向过程”的风格?双方各有支持者。像“对象是低效的”或“对象非常棒”这样的议论也时有耳闻。本文不尝试轻易判定两种方法的哪种具有绝对的优势,而是要找出每种方法的优缺点。
  
  以下是面向过程风格的代码示例:
  
  (做为现在的主流开发语言)
  print "Hello, world.";
  ?>
  
  以下是面向对象风格的代码示例:
  
  (做为现在的主流开发语言)
  class helloWorld {
  function myPrint() {
  print "Hello, world.";
  }
  }
  $myHelloWorld = new helloWorld();
  $myHelloWorld->myPrint();
  ?>
  
  如果你想了解一些“面向对象”的基本知识,请使用Google搜索,网络上有非常多精彩的文章。
  
  谁像这样写代码?
  为了理解为什么这个论题成为论坛上口水战的导火线,我们看一些每个阵营的比较极端的例子。我们看看“过程狂热”和“对象狂热”。看看他们的观点听起来是不是有点熟悉。
  
  过程狂热
  过程狂热曾在上课时被计算机教师批评,因为这种方法没有使用更加抽象的实现方式。而支持面向过程者的观点“它可以工作!”并不能提高其编程水平和档次。毕业后他们可能找到一个工作,写驱动程序,文件系统或其它的偏向底层的编程,他们的注意力集中于速度和代码的精炼。
  
  “过程狂热”极端的例子是抵制对象,抵制抽象化。他们总在想着如何让程序运行起来更快,而不在乎别人是否能读懂他们的代码。他们常常把编程当成竞赛而不是团队活动。除了php(做为现在的主流开发语言)外,他们最喜爱的编程语言是C和汇编。在php(做为现在的主流开发语言)世界中他们可能会开发PECL模块,贡献出高效率的代码。
  
  对象狂热
  对象狂热者热衷于在任何时候使用面向对象的风格来书写代码。他们没有真正考虑过用这种方式是否会影响程序的执行效率。有时候让人觉得他们更享受抽象的设计概念而不是现实的代码。他们通常很可能是项目管理者或文档书写者。
  
  对象狂热者指出,如果没有抽象的设计方法我们仍然在使用0和1进行编程。他们喜欢用伪码来描述问题。极端的例子是对象狂热者即使知道有时候会牺牲效率仍然使用对象。 除了php(做为现在的主流开发语言),他们最喜欢的语言是Java和Smalltalk。在php(做为现在的主流开发语言)世界中,他们可能会开发PEAR模块,贡献文档化非常好,易于维护的代码。
  
  不要偏激和讽刺
  你知道为什么论坛上总是充斥着各种偏见吗?你的经验阅历,你对新事物的态度都可能是原因。作为程序员,我们需要时常注意这些偏见并以开放的心态去学习新事物。
  
  你的编码倾向?
  考虑一下当你书写php(做为现在的主流开发语言)代码时有什么偏好或倾向。通常这些偏好是比较隐晦的。有时候你可能在每个项目中有着同样的偏好。我个人倾向于“优雅”,但我不想在此定义如何才是“优雅”的代码,那应当出现在另一篇文章里。但是,理论化的偏好不一定适合于实际项目—相反地,他们常常是一种偏见。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

See all articles