2

HTML要素を表すクラスの階層があります。ブラウザのバージョンによっては対応していないものもあります。たとえば、HTML5 キャンバスは、バージョン 9 より前の Internet Explorer と互換性がありません。

要素の種類ごとに、それらが呼び出し元のブラウザーでサポートされているかどうかを確認できるようにしたいと考えています。

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 < 9 と非互換にします.

これを防ぐにはどうすればよいですか?

お読みいただきありがとうございます。

-ウイルス

4

2 に答える 2