php使用静态方法设置公共变量

时间:2017-12-12 17:12:00

标签: php variables static

有没有办法使用从另一个类调用的静态方法设置public var,所以稍后我的类Photo我可以将它用作$ this-> type?

class Photo
{
    public $type;

    public static function create($file, $title, $alt, $type): self
    {
        $photo = new static();
        $photo->file = $file;
        $photo->title = $title;
        $photo->alt = $alt;
        $photo->type = $type; // passed string 'gallery'
        return $photo;
    }

    public function myFunc() {
        echo $this->type; // getting null
    }

}

0 个答案:

没有答案