0

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?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a typo. The issue consists also with the changed var name. I edited the original post. Thanks for pointing out.
Making it global does not work 'global $default_value;' does not get rid of the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.