OOP如何管理“包含”存储在不同文件中的类 [英] How does OOP manage to 'include' classes stored in different files

查看:174
本文介绍了OOP如何管理“包含”存储在不同文件中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图进入OOP开发,我转向这里,因为我不喜欢搜索网络,只找到关于什么类是什么以及他们如何可以从彼此继承的非常基本的信息 -

I'm trying to move into OOP development and am turning to here as I'm sick of searching the web and finding only the very basic information about what classes are and how they can inherit from each other - that I understand.

我不明白的是,所有这些类如何存储在不同的文件中,围绕文件夹结构,所有你需要做的使用类只是提到它的名称在你的代码。这是如何工作的完全?

What I don't understand as yet is how all these classes can be stored in different files, doted around a folder structure, and yet all you need to do to use the class is just mention its name in your code. How does this work exactly?

在一个相关的说明,如果任何人可以推荐一本好的书或在线教程,将提供一个良好的基础在OOP(最好基于PHP)我会非常感激。

And on a related note, if anyone can recommend a good book or online tutorial which will provide a good foundation in OOP (preferably php based) I'd be very grateful.

推荐答案

技术答案是 PHP自动加载

实现答案是:

使用与文件夹结构相关的类的名称。有文章在这里,但这里是一个简要的总结:

One common method used has to do with the names of classes relating to folder structure. There are articles out there, but here is a brief summary:

设置自动加载代码时,取类名,并用斜线替换下划线。

When setting up your autoload code, take the class name and replace underscores with slashes. This way you can organize your classes in folders.

例如:

类名:Database_Provider_MySQL

Classname: Database_Provider_MySQL

文件:Database / Provider / MySQL.php

File: Database/Provider/MySQL.php

因此在自动加载中,您将接收传入的类名,替换下划线与斜杠。然后包含该特定文件。

So in autoload, you'd take the incoming classname, replace the underscores with slashes. Then include that specific file.

这实现了你想要完成的任务,你可以通过创建一个新的实例来简单地加载一个类。你永远不必对这些类使用include语句。

This achieves what you are trying to accomplish, you can simply load a class by creating a new instance of it. You never have to use the include statement for these classes.

别记得不要深入到你最终有6个关卡。我认为在3和5之间是一个很好的最大值。

Do remember to not go to deep where you end up with 6+ levels. I think between 3 and 5 is a good maximum.

此外,这要求每个文件只保留1类(类似于Java)。虽然它可能看起来不方便,它使定位代码更容易。

Also, this does require that you keep only 1 class per file (similar to Java). Though it might seem inconvenient, it makes locating code a lot easier.

这篇关于OOP如何管理“包含”存储在不同文件中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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