How to make variable global across entire class

本秂侑毒 提交于 2020-01-15 03:31:04

问题


Very simple question, is it possible to make a variable that is retrieved from outside of a class, 'global' to the whole class so that I do not have to call the 'global $variable' at the beginning of each method?

This is what I am currently doing:

class test{
    public function testing(){
        global $globalVariable,

        // Do something
    }
    public function testing_two(){
        global $globalVariable,

        // Do something
    }
}

In other words, can I import variables into the construct function and therefore make them accessible to the entire class without having to call 'global' for each method?

UPDATE

Not sure if I have made it too clear with what I would like to achieve. Please see below:

$globalVariable = 'hello';

class test{
    public function testing(){
        global $globalVariable,

        // Do something
    }
    public function testing_two(){
        global $globalVariable,

        // Do something
    }
}

回答1:


To clear up how to do it:

You most likely want to do something like this include('database.php');. At that very point everything you included there is global to your script. Now if you have a class like the above you add a constructor:

class testclass
{
    private $db;

    public function __construct($db)
    {
       $this->db = $db;
    }

    public function yourmethod()
    {
       $this->db->prepare(); // And so on
    }
}

Let's assume your global variable is called $db in the global scope. You can just construct your object now using new testclass($db);. When you now use $this->db in all your methods there is no need to use the global statement.




回答2:


Just use a reference:

class test{
    private $myvar;

    //use __ or old syntax to your liking
    function test() {
        global $globalVariable;
        $myvar =& $globalVariable;
    }

    public function testing(){
        //Use $this->myvar

        // Do something
    }
    public function testing_two(){
        //USe $this->myvar

        // Do something
    }
}



回答3:


Pass that variable as a property of the class

class test
{
    private $globalVariable = '';

    public function testing()
    {
        $this->globalVariable = 'set-som-value';
    }

    public function testing_two()
    {
        $this->globalVariable = 'Do Extra Work';
    }
}

and you can change the variable visibility with the words private, public and protected, where private makes the variable accessible only into that class, protected makes the variable available in classes that extends that particular class, and public makes the variable accessible from everywhere.

Global outside the class

global $globalVar;
$globalVar = 'SomeVal';

class test
{
    public function testing()
    {
        global $globalVar;
    }
}



回答4:


This might help. I have a var outside my class I use for mysqli.

             <?php
             $DB_NAME = 'database';
             $DB_HOST = 'host';
             $DB_USER = 'user';
             $DB_PASS = 'password';

             $linkID = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

             if (mysqli_connect_errno()) {
                  printf("Connect failed: %s\n", mysqli_connect_error());
                  exit();
             }


             $Test = new Test($linkID);


                 class Test {

                     public $linkID;

                     function __construct($linkID){ $this->linkID = $linkID; }

                     public function new_mysql($sql) {
                         $result = $this->linkID->query($sql) or die($this->linkID->error.__LINE__);
                         return $result;
                     }

                     public function do_query() {
                         $sql = "SELECT `name` FROM `contacts`";
                         $result = $this->new_mysql($sql);
                         while ($row = $result->fetch_assoc()) {
                             print "Test $row[name]<br>";
                         }
                      }
                 }

         ?>

Now the database details are passed to the class as a helper. Hope this helps.



来源:https://stackoverflow.com/questions/9240694/how-to-make-variable-global-across-entire-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!