Home php教程 php手册 开发大型PHP项目的方法五

开发大型PHP项目的方法五

Jun 13, 2016 am 10:25 AM
php author use Large develop data method Purpose kind conduct

作者:Luis Argerich 译者:limodou 使用类进行数据存储   对于PHP和OOP一件非常好的事情就是,你可以很容易地定义一个类来操作某件事情,并且无论何时你想 用的时候都可以调用相应的类。假设你有一个HTML表单,用户可以通过选择产品ID号来选择一个产品。在数 据库中有产品的信息,你想把产品显示出来,显示它的价格等等。你拥有不同类型的产品,并且同一个动作 可能对不同的产品具有不同的意思。例如,显示一个声音可能意味着播放它,但是对于其它种类的产品可能 意味着显示一个存在数据库中的图片。你可以使用OOP或PHP来减少编码并提高质量:   定义一个产品的类,定义它应该有的方法(例如:显示),然后定义对每一种类型的产品的类,从产品 类派后出来(SoundItem类,ViewableItem类,等等),覆盖在产品类中的方法,使它们按你的想法动作。   根据数据库中每一种产品的类型(type)字段给类命名,一个典型的产品表可能有(id, type, price, description, 等等字段)...然后在处理脚本中,你可以从数据库中取出type值,然后实例化一个名为type 的对象: ----------------------------------------------------- action(); ?>---------------------------------------------------   这是PHP的一个非常好的特性,你可以不用考虑对象的类型,调用$obj的显示方法或其它的方法。使用 这个技术,你不需要修改脚本去增加一个新类型的对象,只是增加一个处理它的类。   这个功能很强大,只要定义方法,而不去考虑所有对象的类型,在不同的类中按不同的方法实现它们,然后在主脚本中对任意对象使用它们,没有if...else,也不需要两个程序员,只有高兴。   现在你同意编程是容易的,维护是便宜的,可重用是真的吗?   如果你管理一组程序员,分配工作就是很简单的了,每个人可能负责一个类型的对象和处理它的类。   可以通过这个技术实现国际化,根据用户所选的语言字段应用相应的类就可以了,等等。 拷贝和克隆   当你创建一个$obj的对象时,你可以通过$obj2=$obj来拷贝对象,新的对象是$obj的一个拷贝(不是一 个引用),所以它具有$obj在当时的状态。有时候,你不想这样,你只是想生成一个象obj类一样的一个新 的对象,可以通过使用new语句来调用类的构造函数。在PHP中也可以通过序列化,和一个基类来实现,但所 有的其它类都要从基类派生出来。 进入危险区域   当你序列化一个对象,你会得到某种格式的字符串,如果你感兴趣,你可以调究它,其中,字符串中有 类的名字(太好了!),你可以把它取出来,象: ----------------------------------------------------- ---------------------------------------------------   所以假设你创建了一个"Universe"的类,并且强制所有的类都必须从universe扩展,你可以在universe 中定义一个clone的方法,如下: ----------------------------------------------------- clone(); ?>---------------------------------------------------   你所得到的是一个新的Something类的对象,它同使用new方法,调用构造函数创建出的对象一样。我不 知道这个对你是否有用,但是Universe类可以知道派生类的名字是一个好的经验。想象是唯一的限制。   注意:我用的是PHP4,我写的有些东西在PHP3下可能不能工作。

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
2 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles