Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 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 Answer

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...