7

I defined a new variable in __construct() and I want to use it in another function of this class. But my variable is empty in the other function!

this is my code:

class testObject{
     function __construct() {
           global $c;
           $data = array("name"=>$c['name'],
                         "family"=>$c['family']);
     }

     function showInfo() {
           global $data;
           print_r($data);
     }

}
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
MajAfy
  • 3,007
  • 10
  • 47
  • 83
  • have you actually looked at : http://uk.php.net/manual/en/language.oop5.php ? – tereško Apr 04 '13 at 12:32
  • Not an answer, but related: http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class/11923384#11923384 – PeeHaa Apr 04 '13 at 12:32
  • 1
    Use class properties instead of `global`: http://www.php.net/manual/en/language.oop5.properties.php Using `global` is (almost?) never a good solution. – str Apr 04 '13 at 12:33
  • Related: http://stackoverflow.com/questions/2206387/what-is-a-class-in-php/2206835#2206835 – Gordon Apr 04 '13 at 12:41

2 Answers2

11

Declare variable $data as global inside the constructor:

 function __construct() {
       global $c;
       global $data;
       $data = array("name"=>$c['name'],
                     "family"=>$c['family']);
 }

Then, it will be visible in other function as well.

Note that extensive usage of global variables is strongly discouraged, consider redesigning your class to use class variables with getters+setters.

A more proper way would be to use

class testObject
{
     private $data;

     function __construct(array $c) 
     {
         $this->data = array(
             "name"=>$c['name'],
             "family"=>$c['family']
         );
     }

     function showInfo() 
     {
         print_r($this->data);
     }

     // getter: if you need to access data from outside this class
     function getData() 
     {
         return $this->data;
     }
}

Also, consider separating data fields into separate class variables, as follows. Then you have a typical, clean data class.

class testObject
{
     private $name;
     private $family;

     function __construct($name, $family) 
     {
         $this->name = $name;
         $this->family = $family;
     }

     function showInfo() 
     {
         print("name: " . $this->name . ", family: " . $this->family);
     }

     // getters
     function getName() 
     {
         return $this->name;
     }

     function getFamily() 
     {
         return $this->family;
     }

}

And you can even construct this object with data from you global variable $c until you elimitate it from your code:

new testObject($c['name'], $c['family'])
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 2
    @MajAfy no, it's not safe because it breaks encapsulation and makes your class depend on these variables to be defined and alterable somewhere in the global scope. It's bad practise. If the class needs $c and $data, use Dependency Injection and store these values as object properties. – Gordon Apr 04 '13 at 12:37
  • @MajAfy - what do you actually mean by "safe"? Using global variables is just a bad practice and should be avoided *if possible*. I've just explained technically why the variable was empty in the other class method. – Alex Shesterov Apr 04 '13 at 12:41
  • 1
    @MajAfy: I've added one more recommendation how you can redesign your class. – Alex Shesterov Apr 04 '13 at 12:56
3

You can do this way. Instead of declaring $data as global variable declare as public or private or protected variable inside the class depending on your use. Then set the data inside _construct.

Using global inside a class is not a good method. You can use class properties.

class testObject{
    public $data;

    function __construct() {
        global $c;
        $this->data = array("name"=>$c['name'],
                        "family"=>$c['family']);
    }

    function showInfo() {
        print_r($this->data);
    }

}
Sabari
  • 6,205
  • 1
  • 27
  • 36