我有代表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