我有代表html元素的类的层次结构。其中一些可能与某些浏览器版本不兼容。以HTML5画布为例,它与版本9之前的Internet Explorer不兼容。PHP - 设置继承的静态属性也会将其设置在继承它的其他类中
对于每种类型的元素,我都希望能够知道它们是否被调用浏览器支持。
abstract class AbstractView // Base class, doesn't represent anything.
{
// ...
// By default, an element will be considered compatible with any version of ny browser.
protected static $FirstCompatibleVersions = array(
'Firefox' => 0,
'Chrome' => 0,
'Internet Explorer' => 0);
protected static function SetFirstCompatibleVersion($browser, $version)
{
static::$FirstCompatibleVersions[$browser] = $version;
}
protected static function IsSupportedByBrowser()
{
$browser = // ... Assumed to be the calling browser name.
$version = // ... Assumed to be the calling browser version.
return static::$FirstCompatibleVersions[$browser] <= $version;
}
}
class CanvasView extends AbstractView // Displays a canvas. Not compatible with IE < 9.
{
// ...
}
CanvasView::SetFirstCompatibleVersion('Internet Explorer', 9);
class FormView extends AbstractView // Displays a form. Assumed compatible with anything.
{
// ...
}
// Nothing to do form FormView.
echo FormView::IsSupportedByBrowser(); // Should print 1 (true) (on firefox 12) but does not.
我的问题是,当我执行:
CanvasView::SetFirstCompatibleVersion('Internet Explorer', 9);
这不仅会设置CanvasView :: $ FirstCompatibleVersion [ 'Internet Explorer的'],但它也将设置这个值对所有其他类,就像这个数组对所有类都是共同的,这使得我所有的元素都与IE <不兼容。
我该怎么做才能防止这种情况?
感谢您花时间阅读。
-Virus
忘记了,如果我重新声明每个子类中的$ FirstCompatibleVersions数组,它可以正常工作。 – Virus721