Martti Laine
Martti Laine

Reputation: 12995

How to add a public var in class in php?

I currently have a error-system like this on my header.php:

include('class.error.php');
Errsys::disable_default();
Errsys::enable_logging('errors.dat');

So, I'm not creating a new object via $asd = new Errsys;. How to add a variable to class, so it can be called like Errsys::variable or via any similar syntax inside or outside the class?

Hope you understood.

Martti Laine

Upvotes: 0

Views: 500

Answers (2)

Jani Hartikainen
Jani Hartikainen

Reputation: 43273

You can create a static variable similar to creating a static function:

public static $whatever;

I would recommend reading PHP manual's section on classes and objects for further information.

ps. If your class is called Errsys, I'd recommend calling the file class.errsys.php as well instead of class.error.php :)

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382909

Which version of php are you using? You can add a class variable like:

class className {
  public $varname;
  ............ more code

In static context:

class className {
  public static $varname;
  ............ more code

Upvotes: 1

Related Questions