PHP语言障碍设计问题

发布于 2024-08-12 08:42:15 字数 1172 浏览 9 评论 0原文

背景

我需要设计一组实用程序类,以简化对多种类型的远程服务(本地、FTP、WebDAV、SVN、SSH)的文件系统访问,但我似乎无法获得“完美”的设计,主要是因为语言障碍 - 我觉得我选择的每条设计路径都被废弃了,因为 PHP 不支持特定功能。

我觉得这很令人沮丧,而且我没有主意。

要求

FileSystem 的基本抽象类或接口,所有其他文件系统类型都继承它。 FileSystem 类(及其子类)是一个“驱动程序”,它实现基本的 FS 操作,例如“移动”、“删除”、“创建”等。

这些驱动程序操作不应暴露给类用户,相反,FileSystem 也是一个根据请求获取文件信息记录的工厂。

文件还基于抽象类或接口FileDirectoryFileSystem 子类的实现者可能会也可能不会子类化它们。 FileSystem 类的用户不“关心”File 的类型(SshFileSvnFile 应该可以工作相同)。

File(和Directory)类应该是与以某种方式创建它的文件系统驱动程序进行对话的类,而用户无法通过<代码>文件系统驱动程序。

我所尝试的

自然地,我将FileSystem创建为一个抽象类,然后继续对File类进行编码,结果发现PHP没有friend支持,因此 File 无法访问 FileSystem 的受保护驱动程序方法。

另一个被废弃的想法是类中的类 - FileSystemFileSystem::File - PHP 没有嵌套类。

FileSystem 分为 FileSystemDriverFileSystemFactory,但这让我陷入了同样的原始问题。

赏金奖励中的 300 声望点

对于 PHP 缺乏封装所需的编程实用程序的原始想法的解决者。

Background

I need to design a set of utility classes that will ease out file system access to several types of remote services (Local, FTP, WebDAV, SVN, SSH), and I cannot seem to get a "perfect" design mostly because of a language barrier - I feel that every design path I have chosen was scraped because PHP did not have support for a specific feature.

I find this frustrating and I'm out of ideas.

Requirements

A base abstract class or interface for FileSystem which every other file system type inherits. The FileSystem class (and it's subclasses) are a 'driver' that implement basic FS operations such as 'move', 'remove', 'create' and so on.

Those driver operations should not be exposed to the class user, instead, FileSystem is also a factory that fetches file info records upon request.

A file is also based on an abstract class or interface File and Directory, which the implementors of a FileSystem subclass may or may not subclass. Users of a FileSystem class do not 'care' about the type of a File (SshFile and SvnFile should work the same).

The File (and Directory) class should be the one to talk to the file system driver that created it in some way without the user being able to do it manually through the FileSystem driver.

What I have tried

Naturally I made FileSystem an abstract class, then proceeded to code the class File and turns out that PHP has no friend support, so File cannot have access to FileSystem's protected driver methods.

Another idea that got scraped was a class inside a class - FileSystem and FileSystem::File - PHP does not have nested classes.

Divide FileSystem into FileSystemDriver and FileSystemFactory, but that gets me into the same original problem.

300 Reputation points in bounty reward

To a solver of an original idea to PHP's lack of programming utilities needed for encapsulation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-08-19 08:42:15

为什么不能将 FileSystemDriver 与 FileSystem 分开?例如:

class SSHFileSystem {   
    private $driver = new SSHDriver();

    function getFile($path)
    {
        return new SSHFile($this->driver, $path);
    }
}

Why can't you separate FileSystemDriver from FileSystem? For example:

class SSHFileSystem {   
    private $driver = new SSHDriver();

    function getFile($path)
    {
        return new SSHFile($this->driver, $path);
    }
}
缺⑴份安定 2024-08-19 08:42:15

文档问题,而不是代码问题。

将文件系统的方法保留为公共方法,但记录它们只能由内部文件和目录对象使用。

这在语言中很常见。 Python 有神奇的方法,或者 _function 表示法等。

如果我是一个用户,我不会只查看每个可用的方法,如果听起来很有趣就开始使用它,我会阅读文档并按照他们所说的去做。

Documentation problem, not code problem.

Leave the FileSystem's methods as public but document that they should only be used by internal File and Directory objects.

This is very common in languages. Python has magic methods, or the _function notation, etc.

If I'm a user, I don't just look at every available method and start using it if it sounds fun, I read the docs and do what they say.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文