Maoristirn
Maoristirn

Reputation: 51

PHP: Use outside variable in static function in class

I would like to use a values stored in a seperate file in a static function in a PHP class.

Example:

<?php
include "vars.php";

class MyClass {
  public static function doSomething() {
    echo "Default value is ".$default_value;
  }
}

MyClass::doSomething();
?>

And in vars.php

<?php
$default_value = "DEFAULT";
?>

I get following error:
Notice: Undefined variable: default_value in C:\xampp\htdocs\mediamanager\new_hp\MyClass.php on line 6
Default value is

How would this be possible? Or is there a better way to read configuration values from a seperate file?

Upvotes: 0

Views: 255

Answers (1)

meskobalazs
meskobalazs

Reputation: 16041

You could declare $default as a global variable using the global keyword, or put it into the GLOBALS superglobal.

Ps: For configuration, I would personally use a class, with constant members.

Upvotes: 1

Related Questions