panthro
panthro

Reputation: 24099

Vars in static classes

I have a var in my class

protected $test = 'test';

Normally I would access this inside the class like:

$this->test;

But this gives me an error.

How can I access vars inside a static class?

Upvotes: 1

Views: 53

Answers (2)

gpupo
gpupo

Reputation: 982

Try this:

//declare
protected $test = 'test';
public $publicTest = 'test';


//access this inside the class
self::$test;


//access this inside the class on dynamic instance
static::$publicTest;

Upvotes: 0

René Höhle
René Höhle

Reputation: 27325

You have to declare the variable as static first.

static $test = 'test';

After that you can access it with self::$test

Upvotes: 3

Related Questions