search
HomeBackend DevelopmentPHP Tutorial 请问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
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.