请问PHP面向对象设计与数据可访问的设计
请教PHP面向对象设计与数据可访问的设计
比如有一个类Person
有一个数据库操作类Conn
Person里面有一个操作是买回来一只鸡,鸡的信息要放到数据库里面去
应该怎么设计?
是在perison里面包涵Conn类,然后实例化Conn对象,访问数据库插入数据,还是怎么弄?
请高手指教!
------解决方案--------------------
+1
LZ应该学习一下设计模式
买鸡(这event有点让我抓狂

业务逻辑确定一下谁(person)、操作(买)、什么(商品/鸡)三者关系
谁(主体)操作(可变)什么(可变)――访问者模式,person作为抽象类,买不是person的必然操作
谁(主体)操作(不变)什么(可变)――原型模式,买是person的必然操作,相当于一个属性
谁(可变)操作(主体且不变)什么(可变)――桥接模式,买操作作为抽象类,导入person买,和买的商品鸡
……
还可以用建造模式,购物车、下单、付费等是顺序关系,作为方法(每个都引入conn),person作为属性
类似有把一次session作为主体抽象类(组合模式),登录、购买、修改资料等其他操作都作为方法(非有序关系),person类则作为抽象类的一个属性
还有其他组合方式,自己去研究吧
但总体来说,conn连接数据库都看不出应该直接从属于person,而是从属于event的一个步骤
所以conn在桥接模式与person、商品两个类并列于抽象类,在原型模式等则二级从属于抽象类
建一个event类内部把conn类实例化为一个方法这样组合为佳
能把工作细分越多,组合方式就越多;当然说的是以类为单位,因为类本身的作用就是合并工作,类下细分反而没意义
上面说的只是我的思维角度,设计模式就是思路,某个模式内还可以有模式,而且每个人想法不同,模式就不同,不是固死的
------解决方案--------------------
设计模式是从大量的实际项目开发中抽象出来的
学习设计模式的目的是让你跳出自己的思维局限,看看人家是怎么做的。仅此而已
在自己的开发实践中,硬是要套上A模式、B模式,不是自己给自己找麻烦吗
------解决方案--------------------
上面的咚咚我也要翻书才叫得上名字,都忘光了
我相信每个人有心学设计模式,最终总会明白自己组合的道理的
唉,人总是这样的,谁不是从小学开始老师让背什么就背什么,等慢慢学习多了,自然就知道原来要自己“组合”招式的
不晓得国外的小学教育是否如此?

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,

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.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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 widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

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

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
