【发布时间】:2016-07-10 15:20:34
【问题描述】:
我正在构建一个 Feed Reader 抽象类,以进一步声明要从各种数据源读取的适配器。在定义扩展类时,我想声明一个属性(格式)仅在选定的子集(在这种情况下为 json、xml)内,即:
abstract Class FeedReader {
public $url;
//This is the line where I would like to define the type, but available only from a subset (json or xml).
abstract function getData();
}
class BBCFeed extends FeedReader {
public $type = 'json'; //I want this value to be restricted to be only json or xml
function getData() {
//curl code to get the data
}
}
在抽象类中声明 $type 最有效(和正确)的方法是什么?我想将 $type 限制在抽象类声明的子集中。
谢谢。
【问题讨论】:
-
abstract function getType()怎么样? -
@D.Dimitrioglo 我认为它仍然允许子类定义任何类型。
标签: php properties abstract