Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-设置继承的静态属性也将在继承它的其他类中设置它_Php_Static_Inheritance - Fatal编程技术网

PHP-设置继承的静态属性也将在继承它的其他类中设置它

PHP-设置继承的静态属性也将在继承它的其他类中设置它,php,static,inheritance,Php,Static,Inheritance,我有一个表示html元素的类的层次结构。其中一些可能与某些浏览器版本不兼容。例如,HTML5画布与9版之前的Internet Explorer不兼容 对于每种类型的元素,我希望能够知道调用浏览器是否支持它们 abstract class AbstractView // Base class, doesn't represent anything. { // ... // By default, an element will be considered compatible w

我有一个表示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::$FirstCompatibleVersion['Internet Explorer'],还将为所有其他类设置此值,就像此数组对所有类都是通用的一样,使我的所有元素与IE<9不兼容

我能做些什么来防止这种情况

感谢您抽出时间阅读


-病毒

您无法阻止这种情况。AbstractView的所有子级共享类静态变量。您可以改为使用对象,或者在每个类的静态变量中设置它们的兼容性,而不是使用SetFirstCompatibleVersion

在静态方法中,您可以使用
get_called_class()
(PHP5.3+)来了解调用它的类

FormView::SetFirstCompatibleVersion()

get\u called\u class()
将返回
'FormView'
。这就是如何区分子类的方法。

忘记了,如果我在每个子类中重新声明$FirstCompatibleVersions数组,它工作得很好。这就是我一直在考虑的问题,但由于我有许多元素(超过100个),我必须在每个子类上添加一个受保护的静态数组。如果有人创建了一个新元素,他可能会忘记创建这个数组,因此objet将共享父类数组(如果我理解得很好的话)。谢谢你的回答@Virus721您仍然需要调用SetFirstCompatibleVersion超过100次,不是吗?有些人也会忘记调用它…不完全是这样,我只想在元素与调用浏览器存在兼容性问题时调用它。大多数HTML4元素不必使用此函数,但一些HTML5元素必须在某些浏览器中使用此函数。但是我仍然需要给我的元素赋予它自己的数组,否则它将使用父元素的类数组,对吗?@Virus721我说的是
class CanvasView extends AbstractView{protected function getFirstCompatibleVersions(){return array('InternetExplorer'=>9);}
您的意思是我应该在基类中放置一个getFirstCompatibleVersion静态函数(它将像您一样返回一个数组),并在存在兼容性问题的类中重写它吗?我尝试用“$class=get_called_class();…$class::…”代替“static::…”,但它似乎具有相同的行为。谢谢anyway@Virus721我认为他建议在FirstCompatibleVersions数组中存储类的名称:
static:$FirstCompatibleVersions[get_called_class()][$browser]=$version。然后您可以使用
return static::$FirstCompatibleVersions[get_called_class()][$browser]@virus721区别在于,使用get_called_class()可以获得一个字符串,您可以用它来区分子类。基本上是meze解释的。看起来很有效,谢谢大家。虽然不能“继承”。如果我说CanvasView与IE<9不兼容,我希望任何扩展CanvasView的类也会自动被认为与IE<9不兼容。但这样就足够了。再次感谢你,西娅@VILUS 721如果这是有帮助的,而你的代表已经达到15考虑投票:
FormView::SetFirstCompatibleVersion()