请教PHP面向对象设计与数据可访问的设计
比如有一个类Person
有一个数据库操作类Conn
Person里面有一个操作是买回来一只鸡,鸡的信息要放到数据库里面去
应该怎么设计?
是在perison里面包涵Conn类,然后实例化Conn对象,访问数据库插入数据,还是怎么弄?
请高手指教!
回复讨论(解决方案)
class Person extends Conn{
function byji(){
$conn=new Conn;//实例化数据库类
$conn->add();//插入数据库
}
}
class Person extends Conn{
function byji(){
$conn=new Conn;//实例化数据库类
$conn->add();//插入数据库
}
}
很高兴你能回复我,我也是这么想的,但是我的同事说最好在类里面不要写与数据库有关的代码
不知道是不是算MVC模式什么的?
class Person extends Conn{
function byji(){
$conn=new Conn;//实例化数据库类
$conn->add();//插入数据库
}
}
这种情况用组合模式还是比较好吧。继承用在这里是不合适的。
引用 1 楼 nowphp 的回复:class Person extends Conn{
function byji(){
$conn=new Conn;//实例化数据库类
$conn->add();//插入数据库
}
}
这种情况用组合模式还是比较好吧。继承用在这里是不合适的。 恩受教了,那如何写呢。
你可以把数据库的操作类conn封装成为一个数据库的操作类,直接在person中调用conn的静态方法add
引用 1 楼 nowphp 的回复: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类实例化为一个方法这样组合为佳
能把工作细分越多,组合方式就越多;当然说的是以类为单位,因为类本身的作用就是合并工作,类下细分反而没意义
上面说的只是我的思维角度,设计模式就是思路,某个模式内还可以有模式,而且每个人想法不同,模式就不同,不是固死的
呵呵,怎么简单就怎么来
不要上人家的圈套
呵呵,怎么简单就怎么来
不要上人家的圈套
哈哈,good advice
设计模式是从大量的实际项目开发中抽象出来的
学习设计模式的目的是让你跳出自己的思维局限,看看人家是怎么做的。仅此而已
在自己的开发实践中,硬是要套上A模式、B模式,不是自己给自己找麻烦吗
上面的咚咚我也要翻书才叫得上名字,都忘光了
我相信每个人有心学设计模式,最终总会明白自己组合的道理的
唉,人总是这样的,谁不是从小学开始老师让背什么就背什么,等慢慢学习多了,自然就知道原来要自己“组合”招式的
不晓得国外的小学教育是否如此?
谢谢xuzuning和snmr_com的指导,我太局限MVC里面了,有自己的设计模式那才是最好的,不用拘泥于某一种模式。

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

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

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

Alipay PHP...

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
