【发布时间】:2014-03-30 20:22:23
【问题描述】:
我需要针对我的简单项目的建议。这是一个记录器系统。我有以下文件/类
文件 - 检查文件是否存在是否可读等
ConfigParser - 它是一个抽象类,我使用 __construct 方法来做一些事情。该类从不直接实例化。这是一个装饰器。
abstract class ConfigParser
{
protected $filename;
public function __construct($file)
{
// Here I want to invoke validate() method of File class
// I know that I can do $file = new File(); $file->validate()
// but the idea is to keep the things decoupled
// Also I know that I can extend the File class ConfigParser extends File
// but this doesn't make sence
// I can pass the File object on child class new XmlParser('file.xml', $fileObj)
// but this doesn't make sence too.
// I don't want to use traits as it's bizarre for me
}
}
XmlParser - 扩展 ConfigParser
IniParser - 扩展 ConfigParser
由于项目目标是保持类解耦,我不知道如何在这种情况下实现 File 类。
【问题讨论】:
-
我想知道这是否更适合codereview.stackexchange.com
-
您可能会发现此答案相关:stackoverflow.com/a/18682856/727208
标签: php oop object design-patterns