请教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











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.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.
