make variable available to all classes, methods, f

2019-06-15 00:51发布

This question seems simple enough, but I can't find an answer anywhere...

At the beginning of my php script/file I want to create a variable.

$variable = 'this is my variable';

I want this variable to be available within the entire script, so that all classes, methods, functions, included scripts, etc. can simple call this variable as $variable.

Class MyClass
{
  public function showvariable()
  {
     echo $variable;
  }
}

The $_SESSION, $_POST, $_GET variables all behave like that. I can just write a method and use $_POST and it'll work. but if I use $variable, which I have defined at the beginning of my script, it says "Notice: Undefined variable: variable in ..." It even says it, when I write a function, rather than a class method...

I tried to write

global $variable = 'this is my variable';

But then the script wouldn't load... "HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request."

How can I make a variable truly globally accessible like $_POST?

Why would I need this? I specifically plan on using it as a form token. At the top of the page I generate my form token as $token, at the bottom of the page I store it in the session. Before handling any form I check if the SESSION token matches the POST token... And since I often have multiple forms on the page (login form in the top nav, and register form in the main body) i just wanna make it convenient and call $token on each form. Sometimes my formelements are generated by classes or functions, but they don't recognize the variable and say it's not defined.

标签: php oop scope
4条回答
爷的心禁止访问
2楼-- · 2019-06-15 00:55

In PHP, global variables must be declared as such within each function in which they will be used. See: PHP Manual, section on variable scope

That being said, global variables are often a bad idea, and in most cases there's a way to avoid their use.

查看更多
贼婆χ
3楼-- · 2019-06-15 01:09

Just define a variable outside of any class or function. Then use keyword global inside any class or function.

<?php

// start of code

$my_var = 'Hellow Orld';

function myfunc() {
  global $my_var;
  echo $my_var; // echoes 'Hellow Orld';
}

function myOtherFunc() {
  var $my_var;
  echo $my_var; // echoes nothing, because $my_var is an undefined local variable.
}


class myClass {

  public function myFunc() {
    global $my_var;
    echo $my_var; // echoes 'Hellow Orld';
  }

  public function myOtherFunc() {
    var $my_var;
    echo $my_var; // echoes nothing.
  }

}

myFunc(); // "Hellow Orld"
myOtherFunc(); // no output
myClass->myFunc(); // "Hellow Orld"
myClass->myOtherFunc(); // no output

// end of file
?>
查看更多
啃猪蹄的小仙女
4楼-- · 2019-06-15 01:16

I found it... -.- I thought it would be easy enough...

I have to call to the variable by using

$GLOBALS['variable'];

Just found it... finally... http://php.net/manual/en/reserved.variables.globals.php

EDIT like 8 months later or so:

I just learned about CONSTANTS!

define('name', 'value');

They just can't be reassigned... I guess I could use that, too! http://www.php.net/manual/en/language.constants.php

查看更多
走好不送
5楼-- · 2019-06-15 01:20

you need to declare global to access it in any function scope:

at the top of your script:

global $var;
$var= "this is my variable";

in your class

...
public function showvariable(){
    global $var;
    echo $var;
}
...
查看更多
登录 后发表回答