用封装类来合理的设计PHP项目--谈PHP项目中类的封装_PHP
编码对于合格的PHP程序员来说并不是什么难事(也许只是花费时间长短的问题),因此系统分析和设计这一阶段就显得尤为重要。不过本文并不打算讨论和需求分析、获取商业逻辑相关的话题,而是针对系统设计方面进行探讨。
编码对于合格的PHP程序员来说并不是什么难事(也许只是花费时间长短的问题),因此系统分析和设计这一阶段就显得尤为重要。对于一个担任PHP项目的系统分析员来说,面临着两个难题:
- PHP语言本身的限制。
这一点在复杂系统的面向对象设计中尤其显著。PHP的面向对象特性在现有版本中虽然得到了改善,但是还不甚健全,根本不足以担任面向对象设计的实现语言;即使眼光长远一些,在即将释出的以Zend Engine 2.0支持的全新PHP中,面向对象特性也不会像现在流行的Java或者C++那样(关于这方面的内容可以参见我在developerWorks中国网站发表的另一篇文章)。但是如果采用完全面向过程(准确说是面向Web页面)的方式,可以想见整个系统的设计会非常复杂,而由此带来的编码复杂和维护困难更加难以应付。 - 现有资料的严重缺乏。
这是众所周知的现象即针对Web项目的系统设计资料不足;而在这些有限资料中,关于PHP的设计资料又非常匮乏。如果本公司或本人也没有相关的技术积累,系统分析员只能在黑暗中摸索方法(更坏的两种情况,一是照搬其他项目比如Java或者C++的设计,二是认为项目简单而不负责任的草草了事)。
既然如此,采用何种方法妥善处理PHP系统的分析和设计?最初的构想应该需要分清项目承担任务的类型:
- 涉及大量客户本身或者客户所在行业的商业逻辑的项目,包括办公系统、订单系统以及其他商业系统。
- 简单网站项目,包括一些需要承担高访问量或要求快速响应的项目比如品牌网站或者活动网站以及其他一些网站。
- 综合性网站项目。通常包含多个相对独立的子系统比如新闻子系统、论坛子系统、产品陈列子系统等等。
PHP的设计初衷在于解决后两种项目的迫切需求,语言本身对于这些项目进行了良好的改造。而众多的PHP开发者对这些项目也具有或多或少的经验,相关书籍中的范例也大都围绕于此。相对说来第一种系统所有的资料不多,各种出版物对其内容也很少提及。因此在本文中将题所述对第一种类型的项目进行详细讲述(有关MVC模式和类封装),同时附带提及第二种项目(有关黑客代码)以及第三种项目的设计方法。当然,并不是被归类的这些项目就只能采用本文描述的方式,系统分析员需要权衡各方面因素加以选择。
如何分离用户界面和后台操作?如何避免将商业逻辑混淆于一般的流程控制中?作为一个严谨的商用项目,就需要考虑很多类似的问题。对于由PHP担当的这类项目,贯彻Model-View-Controller(MVC)模式的设计是一个非常好的方法。
理论描述
在这里我不想多加解释MVC模式本身--简单的从字面上以及应用上说,通过将系统的设计分为Model模型/逻辑、View视图/界面、Controller控制/流程三个逻辑部分达到良好的项目效果,以此便利各部分开发者的工作并降低日后的维护成本。(如果您熟悉JSP开发的Model 2模式,可以发现它也是MVC模式的很好体现。)就现实的项目开发而言,现存的很大问题包括网页设计人员和程序开发人员的工作交错和冲突以及商业逻辑嵌入页面造成不可重用也很难维护等等。引入MVC模式一方面可以为系统的总体设计指出明确的方向,对于开发团队的分工也是良好的指导。
既然依照MVC模式要求对系统的总体结构在逻辑上分成三部分,那么团队的开发者中也存在着针对各个部分的开发者。
开发者角色 | 相关系统逻辑 | 职责 |
网页设计人员 | View视图/界面 | 设计所有用户界面的网页模板。 |
控制流程开发人员 | Controller控制/流程 | 编写系统流程中的所有PHP页面。 |
商业逻辑开发人员 | Model模型/逻辑 | 开发系统设计中规定的各个类(其中的方法)。 |

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement data backup and recovery functions in PHP projects? In the process of developing and managing PHP projects, data backup and recovery functions are very important. Whether it is to avoid accidental data loss or to ensure data security during project migration and upgrade, we need to master data backup and recovery methods. This article will introduce how to implement data backup and recovery functions in PHP projects. 1. Data backup definition backup path: First, we need to define the path used to store backup files. Can be defined in the project configuration file

Deployer introductory tutorial: Optimizing the deployment process of PHP projects Introduction: In modern development, automated deployment has become an indispensable part. Deployer is an excellent PHP project deployment tool, which can help us simplify the deployment process and improve efficiency. This article will introduce the basic use of Deployer, and show through code examples how to optimize the deployment process of PHP projects through Deployer. 1. Why choose Deployer? Deployer is a lightweight

How to implement verification code and prevent bot attacks in PHP project? With the development and popularity of the Internet, more and more websites and applications are beginning to be threatened by bot attacks. In order to protect user information security and provide a good user experience, developers need to implement verification codes and measures to prevent bot attacks in their projects. This article will introduce how to implement verification codes and prevent bot attacks in PHP projects. 1. Implementation of verification code Verification code is a common method to prevent robot attacks. Users need to enter a verification code when logging in or registering.

How to implement user authentication and permission control in PHP projects? In modern web applications, user authentication and permission control are one of the most important functions. User authentication is used to verify the user's identity and permissions, while permission control determines the user's access permissions to various resources in the system. Implementing user authentication and permission control in PHP projects can protect the security of user data and ensure that only authorized users of the system can access sensitive information. This article will introduce a basic method to help you implement user authentication and permission control functions to ensure

How to implement image processing and watermark addition in PHP project? In recent years, with the rapid development of the Internet, the use of images has played an increasingly important role in web design and application development. In order to meet users' needs for high-quality images, we need to implement image processing and watermark adding functions in the PHP project. This article will introduce a simple and effective method to achieve this goal. 1. PHP’s image processing functions PHP provides a series of functions for image processing, which can help us zoom, crop, rotate, etc. images.

Introduction to the automatic deployment tutorial of PHP projects based on Deployer: When developing PHP projects, we often need to deploy code to the server. Traditional deployment methods may involve tedious steps such as manually uploading files and backing up databases. To increase efficiency and reduce errors, we can use automated deployment tools. Deployer is a powerful automated deployment tool for PHP projects, which can help us deploy code and configure servers quickly and reliably. This article will introduce how to use Deploye

How to perform performance tuning and resource optimization in PHP projects? With the rapid development of the Internet, more and more applications use PHP as a development language. Due to PHP's ease of use and flexibility, many developers choose to use it to build their websites and applications. However, due to the dynamic and interpreted nature of PHP, some developers may face performance issues. This article will explore how to perform performance tuning and resource optimization in PHP projects to improve application performance and responsiveness. 1. Use appropriate data structures

Memcache is an open source, distributed caching technology. It greatly improves the speed of data access by storing data in memory, thus improving the performance and responsiveness of the website. In PHP projects, Memcache caching technology is also widely used and has achieved good results. This article will deeply explore the application and practice of Memcache caching technology in PHP projects. 1. Principles and advantages of Memcache Memcache is a memory caching technology that can store data
