Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a variable from __construct() in other methods

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);
     }

}
like image 605
MajAfy Avatar asked Apr 04 '13 12:04

MajAfy


People also ask

What is function __ construct ()?

PHP - The __construct FunctionA constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

What does __ construct mean?

__construct() is the method name for the constructor. The constructor is called on an object after it has been created, and is a good place to put initialisation code, etc. class Person { public function __construct() { // Code called for each new Person we create } } $person = new Person();

What is construct and destruct in PHP?

You can say that the Constructors are the blueprints for object creation providing values for member functions and member variables. Once the object is initialized, the constructor is automatically called. Destructors are for destroying objects and automatically called at the end of execution.


2 Answers

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'])
like image 177
Alex Shesterov Avatar answered Sep 30 '22 02:09

Alex Shesterov


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);
    }

}
like image 28
Sabari Avatar answered Sep 30 '22 01:09

Sabari