1

I have a class in php. Inside class I have a function called setContent(). Inside that I have multiple variable with values. I have taken those variable values and stored them in an array.In another function I wanted to get all the variables stored in an array. So my code is like this

class foo{
    public function setContent() {
        global $my_array;
        $var1 = 'variable 1';
        $var2 = 'variable 2';
        $var3 = 'variable 3';
        $var4 = 'variable 4';
        $var5 = 'variable 5';
        new_array = array();
        $my_array = array_push($new_array, $var1, $var2, $var3, $var4, $var5);
    }

    public function getContent() {
        global $my_array;
        var_dump($my_array);
    }
}

But when I am doing var_dump($my_array). Its showing NULL. So can someone tell me how to get those variables inside other function in an array. Any help and suggestions will be really appreciable.

3
  • you should declare variable outside functions, global $my_array = array(); Commented Dec 30, 2014 at 13:53
  • This is circumventing everything that classes are designed for. Don't use globals. Store the variable as a property of the class. Commented Dec 30, 2014 at 13:54
  • Since you're using classes, read about the full scope of possibilities, in particular properties: php.net/manual/en/language.oop5.properties.php Commented Dec 30, 2014 at 13:54

3 Answers 3

5

You can set the array for use by declaring it inside your class. Then, you just refer to $this rather than the global var.

class foo{
    public $my_array; # new bit here

    public function setContent() {
        # global $my_array; take this bit out
        $var1 = 'variable 1';
        $var2 = 'variable 2';
        $var3 = 'variable 3';
        $var4 = 'variable 4';
        $var5 = 'variable 5';
        $new_array = array();
        $this->my_array = array_push($new_array, $var1, $var2, $var3, $var4, $var5);
    }

    public function getContent() {
        var_dump($this->my_array);
    }
}

Because the array is now part of the foo object, we can now refer to it all along. If you were to keep it public you could do something like the following:

$f = new foo();
print_r($f->my_array);
Sign up to request clarification or add additional context in comments.

1 Comment

Supply a description and this answer would be perfect.
2

Why don't you implements your class like these :

class foo{

    public $my_array = array();

    public function setContent() {
        $var1 = 'variable 1';
        $var2 = 'variable 2';
        $var3 = 'variable 3';
        $var4 = 'variable 4';
        $var5 = 'variable 5';

        array_push($this->my_array, $var1, $var2, $var3, $var4, $var5);
    }

    public function getContent() {
        var_dump($this->my_array);
    }
}

I think this is a bad practice to use global variables in OOP.

In fact i recommend to rewrite your getContent function :

public function getContent() {
    return $this->my_array;
}

and instanciate your object :

$my_object = new foo();
$foo->setContent();
var_dump ($foo->getContent());

I will improve class reusability

Comments

-1

http://php.net/manual/fr/function.array-push.php

You've to write array_push($new_array, $var1, $var2, $var3, $var4, $var5);

because array_push return the length of the array (an integer, not the array !). So you can't write:

$myarray = array_push($myarray, $var1, $var2, $var3, $var4, $var5);

Comments

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.