Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

php - PHP | 访问未声明的静态属性(PHP | Access to undeclared static property)

After trying several attempts at this for some reason i get the error Access to undeclared static property when i try to make an object out my class.

(由于某种原因对此进行了几次尝试后,当我尝试在类中创建对象时,出现错误: Access to undeclared static property 。)

My class:

(我的课:)

final class repo {
    var $b;

    /**
     * @var GuzzleHttpClient
     */
    protected $client;

    function repo($myvar)
    {
        static::$b = $myvar;
        $this->client = $b;
    }
}

Me making an object:

(我在做一个对象:)

$myobj = new repo("test");
  ask by Jimmy KA translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should declare $b as static variable.

(您应该将$ b声明为静态变量。)

Also note that method as a class name is deprecated now see the details here

(另请注意,现在不建议使用方法作为类名, 请参见此处的详细信息)

final class repo {
    public static $b;

    /**
     * @var GuzzleHttpClient
     */
    protected $client;

    function repo($myvar)
    {
        static::$b = $myvar;
        $this->client = static::$b;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...