PHP中的OOP方法 [英] OOP approach in PHP

查看:77
本文介绍了PHP中的OOP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用PHP进行程序编程(这甚至是一个单词?)已有五年了,决定尝试使用OOP方法,但遇到了一些概念/设计问题.假设您在程序中有一些模块,每个模块都可以列出,添加,编辑和删除实体.实体可以是..dunno,用户,客户,产品等.

I'm programming in PHP procedurally (is this even a word?) for about five years now and decided to try an OOP approach but ran into some concept/design problems. Let's say you have some modules in the program, every module has the possibility to list, add, edit and delete an entity. An entity can be..dunno, user, client, product etc.

您将如何设计类来操纵这些实体?

How would you design the classes to manipulate these entityes?

我想到了两种可能性:

  • 使用诸如getUsersList,addUser,editUser,delUser之类的方法为每个实体创建类. 这种方法似乎很耗资源,因为在清单的脚本中,您仅需要getUsersList甚至delUser方法,而在添加用户弹出脚本中,您仅需要addUser方法,而在编辑用户弹出脚本中仅需要editUser方法.因此,您必须实例化一个对象,并且只能使用它的两个或一个方法...
  • 创建通用类:以这种方式为每个实体列出,添加,编辑,删除和扩展它们,而您一次只需实例化一个类(您真正需要的一个类)
  • create classes for every entity with methods like getUsersList, addUser, editUser, delUser
    This approach seems resource consumingbecause in the script for the listing you only need the getUsersList and maybe delUser methods, while in the add user popup script, you only need the addUser method and in the edit user popup script only the editUser method. So, you have to instanciate an object and only use two or one of it's methods...
  • create general classes: listing, add, edit and delete and extend them for every entity this way you only have to instanciate one class at a time (the one you really need)

预先感谢

推荐答案

我将创建一个实现此接口,则必须在这些类中定义接口中的方法.

I would create an interface defining your list, add, edit, and delete methods. This gives you a class "template". If your classes (User, Client, Product, etc.) implement this interface, then the methods in the interface must be defined in those classes.

这将为您提供一个类似的"API",以访问实现您的接口的每个类的所有功能.由于列出的每个对象都包含不同的数据,因此方法的详细信息将有所不同,因此是分开的,但是接口将是相同的.

This will give you a similar "API" to access all the functionality of every class that implements your interface. Since each of your listed objects contains different data, the details of the methods will be different, and thus separate, but the interface will be the same.

旁边:

您在方法列表中包含列表"使我有些担心.似乎暗示您将对象视为用户,客户端,产品等的集合,其中很可能应该有一个User类代表一个单个用户,一个Client类代表一个单个客户端

Your inclusion of "list" in your list of methods concerns me a little. It seems to imply that you are seeing your objects as collections of Users, Clients, Products, etc, where there should most likely be a User class that represents a single user, a Client class that represents a single client, etc.

另一方面,列表"可以作为静态方法处理-可以在没有类实例的情况下调用的方法.

On the other hand, "list" may be handled as a static method - a method that can be called without an instance of the class.

$bob = new User('bob');
$bob->add(); // Adds bob to the database
$fred = new User('fred');
$fred->add(); // Adds fred to the database

$users = User::list(); // Gives an array of all the users in the database

无论如何,这就是我要处理的事情.

That's how I would handle things, anyway.

这篇关于PHP中的OOP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆