微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将过程PHP转换为面向对象的PHP

我目前有一个相当大的应用程序完全用程序 PHP编写.我希望进一步提高我的 PHP体验,并使用面向对象技术重新编写我的大部分应用程序.

OOP有很多方面可以帮助减少代码量并使其更易于阅读.但是,我有几个问题.

1)据我所知,一个类被用作任意数量对象的蓝图,但任何一个类只代表一个对象,从不多于一个.因此,一个班级可以代表一个玩家,但从不代表多个玩家.

2)由于我要包含很多不同的类,我是否使用“Loader”类使用spl_autoload_register加载它们,或者我只是在我的应用程序的程序文件中使用spl_autoload_register?

编辑:所以我的自动加载器将是一个类,然后我创建一个启动自动加载的实例或只是一个带有该函数和我将包含的spl_autoload_register的PHP文件,以避免在多个文件中重复相同的代码

3)我的一些课程依赖于其他课程.我以前从未遇到过这个,所以老实说我不知道​​答案.如果我在我的主程序文件中包含所有类,但我的播放器类不包含它需要运行的类,那么播放器类是否可以工作,因为主程序包含了播放器所依赖的类?

编辑:所以即使一个类可以实例化一个Player类型的对象,并且该类没有直接包含Player类,它仍然可以工作,因为控制器类确实包含了Player类?

4)在多种情况下,我需要处理我正在创建的对象.我想知道我应该怎么做.例如,在我的Player类中,我有时需要从一个Player向另一个Player发送一些东西.那么,我是否在Player类中实现一个静态方法,该方法将两个Players作为参数并进行传输或者我还要做其他事情吗?

编辑:好的,所以避免使用静态方法.现在我遇到了一个严重的问题:我的应用程序中有多次运行的方法,但我不能将它们作为静态方法实现.我应该将它们作为实例方法实现吗?例如,从一个播放器发送到另一个播放器.我是否会创建一个实例方法获取Player对象并将其发送给它或从中发送?

5)我有很多方法并不真正属于任何一个类的实例,它们也不适合作为静态方法.这些是否应该作为Common或类似的静态方法在他们自己的类中声明?在这种情况下,在实践中做了什么?

编辑:这些方法是否属于使用它们的特定应用程序文件,或者可能存储在自己的“functions.PHP文件中?

6)我想学习如何使用命名空间,但我的代码永远不会被其他人使用,我永远不会在我的应用程序中使用任何其他人的代码.名称空间是我的应用程序中不必要的补充,还是学习如何使用它们是一个好主意?无论如何,一个应用程序是否有一个命名空间(应用程序名称?)或每个类是否属于它自己的命名空间?

7)最后,有一个类用于数据库连接,还有一个用于网络方法的类是常见的吗?我的应用需要两者.我认为转换我的代码以使用面向对象技术时遇到的主要问题是确定将哪些方法放在哪里,因为目前它们都在一个整体文件中.

感谢您提供的任何帮助和见解.

1) It is my understanding that one class is used as a blueprint for any number of objects,but any one class represents only one object,never more than one. So one class Could represent a player,but never multiple players.

通常,您还将拥有表示对象集合的类,例如一个“玩家”类,可用于从所有玩家的集合中检索单个玩家:

$players = new Players();
$john = $players->findByName("john");

2) Since I will have quite a few different classes to include,do I use a “Loader” class to load them all using spl_autoload_register or do I just use spl_autoload_register in the program files for my application?

这很大程度上取决于项目的复杂性.一个简单的自动加载器功能通常是足够好的,但你可以看看Zend Framework Autoloader class.

3) Some of my classes depend on other classes. I’ve never encountered this before,so I honestly do not kNow the answer. If I include all the classes in my main program file,but my player class does not include the class which it needs to function,will the player class work since the main program has included the class which player depends on?

是.但是,通过自动加载,您根本不需要担心这一点.如果不使用自动加载,最好在定义类的文件中包含必需的类(使用require_once()以避免多次包含同一文件).

4) There are multiple cases where I will need to work on the objects I am creating. I am wondering how I should do that. For example,in my Player class I will sometimes need to send something from one Player to the other Player. So,do I implement a static method in the Player class that takes two Players as parameters and does the transfer or do I do something else?

静态方法几乎总是错误方法.普通方法属于一个实例(即特定玩家),静态方法属于该类,即玩家的一般“想法”.如果你需要将东西从一个玩家转移到另一个玩家,为什么不这样实现:

class Player {
    public function transferMoney(Player $recipient,$amount) { ... }
}

$tom = new Player("tom");
$marc = new Player("marc");

$tom->transferMoney($marc,500);

5) I have a lot of methods that don’t really belong to any one instance of a class nor are they really appropriate as static methods. Should these be declared in their own class as static methods as Common or similar? What is the done in practice in this situation?

我无法合理地回答这个问题.但是,PHP中仍然存在普通函数,这些函数似乎是这种情况的最佳方法.然而,如果你正确地做OOP,你很可能永远不会遇到这样的问题.这通常是您的类设计的一个问题,这使您认为这些方法不属于任何对象.

6) I’d like to learn how to use namespaces,but my code will never be used by others and I will never use anyone else’s code in my application. Are namespaces an unncessary addition in my application or would it be a good idea to learn how to use them? Regardless,does one application have one namespace (the application name?) or does each class belong to it’s own namespace?

命名空间很棒但是如果没有它们,你的代码可能会很好.由于命名空间可以嵌套,因此每个组件通常都有一个顶级命名空间和子命名空间.

7) Lastly,is it common to have one class for database connections and also a class for network methods? My application needs both. I think the main problem I am having with converting my code to use object-oriented techniques is determining which methods to put where,as currently they are all in one monolithic file.

这取决于您如何建模您的实际情况.如果数据库连接和“网络”是两个不同的东西,那么两个类就是可行的方法.

原文地址:https://www.jb51.cc/php/137424.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐