静态函数行为和__toString魔术方法


Static function behavior and __toString magic method

下面是我的代码的一个片段

namespace classes'tools'html;
class html{
    public static function element($tag) {
        return new element($tag);
    }
}
class element{

      //......
    public function __toString() {

        $element = "<$this->tag";
        foreach ($this->attributes as $attribute => $value) {
        $element.= " $attribute='"$value'"";
        }
        if ($this->selfClosing) {
            $element.="/>";
            return $element;
        }
        $element.=">$this->html</$this->tag>";
        return $element;
    }
}

$html = new html();

让我困惑的是,如果我用调用元素

$html::element('a')->attr('href','/home/')->html('home');

它将返回

'<a href="/home/">home</a>"

但如果我用来称呼它

$html->element('a')->attr('href','/home/')->html('home');

它将返回一个element对象

所以我的问题是,为什么用::运算符静态调用它会调用__toString方法,而后者不会?

编辑

链接到完整元素类

html类的element()函数是静态的,所以您不想创建html的实例,而是这样调用您的函数:

echo html::element('a')->...

有时需要强制转换返回值以获得字符串,因为元素函数返回一个元素对象:

$var = (string)html::element('a')->...

注意:我不使用$html