Home Backend Development PHP Tutorial 请问PHP面向对象设计与数据可访问的设计

请问PHP面向对象设计与数据可访问的设计

Jun 13, 2016 pm 12:50 PM
event nbsp php

请教PHP面向对象设计与数据可访问的设计
比如有一个类Person
有一个数据库操作类Conn
Person里面有一个操作是买回来一只鸡,鸡的信息要放到数据库里面去
应该怎么设计?
是在perison里面包涵Conn类,然后实例化Conn对象,访问数据库插入数据,还是怎么弄?
请高手指教!

php 面向对象,数据库
------解决方案--------------------
引用:
引用:class Person extends Conn{
   function byji(){
      $conn=new Conn;//实例化数据库类
      $conn->add();//插入数据库
   }
}

这种情况用组合模式还是比较好吧。继承用在这里是不合适的。


+1
LZ应该学习一下设计模式
买鸡(这event有点让我抓狂)从业务逻辑看不是继承关系,可能你的项目是这样也说不定

业务逻辑确定一下谁(person)、操作(买)、什么(商品/鸡)三者关系
谁(主体)操作(可变)什么(可变)――访问者模式,person作为抽象类,买不是person的必然操作
谁(主体)操作(不变)什么(可变)――原型模式,买是person的必然操作,相当于一个属性
谁(可变)操作(主体且不变)什么(可变)――桥接模式,买操作作为抽象类,导入person买,和买的商品鸡
……
还可以用建造模式,购物车、下单、付费等是顺序关系,作为方法(每个都引入conn),person作为属性
类似有把一次session作为主体抽象类(组合模式),登录、购买、修改资料等其他操作都作为方法(非有序关系),person类则作为抽象类的一个属性
还有其他组合方式,自己去研究吧

但总体来说,conn连接数据库都看不出应该直接从属于person,而是从属于event的一个步骤
所以conn在桥接模式与person、商品两个类并列于抽象类,在原型模式等则二级从属于抽象类
建一个event类内部把conn类实例化为一个方法这样组合为佳
能把工作细分越多,组合方式就越多;当然说的是以类为单位,因为类本身的作用就是合并工作,类下细分反而没意义

上面说的只是我的思维角度,设计模式就是思路,某个模式内还可以有模式,而且每个人想法不同,模式就不同,不是固死的
------解决方案--------------------
设计模式是从大量的实际项目开发中抽象出来的
学习设计模式的目的是让你跳出自己的思维局限,看看人家是怎么做的。仅此而已

在自己的开发实践中,硬是要套上A模式、B模式,不是自己给自己找麻烦吗

------解决方案--------------------
上面的咚咚我也要翻书才叫得上名字,都忘光了
我相信每个人有心学设计模式,最终总会明白自己组合的道理的

唉,人总是这样的,谁不是从小学开始老师让背什么就背什么,等慢慢学习多了,自然就知道原来要自己“组合”招式的
不晓得国外的小学教育是否如此?
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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

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 ?

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

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

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