2

I'm trying to access 3 variables from 3 different functions in a single function. They're all arrays. I want to foreach them in that single function.

class poetry {
    public function __construct() {
        add_action( 'wp_head', array( $this, 'generate_scripts' ) );
    }

    public function page_one() {
        $options = array(
            array( 'content1', 'Content1', 'The Content 1'),
            array( 'content2', 'Content2', 'The Content 2'),
            array( 'content3', 'Content3', 'The Content 3'),
        );

        $this->add_settings( 'page_one', $options );
    }

    public function page_two() {
        $options = array(
            array( 'content4', 'Content4', 'The Content 4'),
            array( 'content5', 'Content5', 'The Content 5'),
            array( 'content6', 'Content6', 'The Content 6'),
        );

        $this->add_settings( 'page_two', $options );
    }

    public function page_three() {
        $options = array(
            array( 'content7', 'Content7', 'The Content 7'),
            array( 'content8', 'Content8', 'The Content 8'),
            array( 'content9', 'Content9', 'The Content 9'),
        );

        $this->add_settings( 'page_three', $options );
    }

    public function generate_scripts() {
        // how do I access $options from the above functions in this function?
        foreach($options as $option) {
            echo $option[0];
        }
    }

}

How can I access all 3 $options from the above functions and foreach the arrays in generate_scripts()?

J. Doe
  • 63
  • 7

2 Answers2

1

You need to get the variables within the same scope as the function you want to use them in. You can either set variables of the class using $this, or you can return the array. Either way, you need to call it first.

Here's an example on how you can create them all variables as properties of the class, and using $this. Note that you need to call the method to populate the variable before you can use it.

class poetry {
    private $page_one; // Initialize variable first

    public function __construct() {
        add_action( 'wp_head', array( $this, 'generate_scripts' ) );
    }

    public function page_one() {
        $this->page_one = array(
            array( 'content1', 'Content1', 'The Content 1'),
            array( 'content2', 'Content2', 'The Content 2'),
            array( 'content3', 'Content3', 'The Content 3'),
        );
    }

    /* Rest of class */

   public function generate_scripts() {
        $this->page_one(); // Call method, so variable holds the values
        $options = $this->page_one;
        foreach($options as $option) {
            echo $option[0];
        }
    }
}

Alternatively, you can use return each array, and use that as the $options value. If you need to use it multiple times inside the function, you can return $options; at the end - after doing everything you need. This way, the method returns the array - and you can use it as a variable inside as much as you need!

public function page_one() {
    $options = array(
        array( 'content1', 'Content1', 'The Content 1'),
        array( 'content2', 'Content2', 'The Content 2'),
        array( 'content3', 'Content3', 'The Content 3'),
    );

    $this->add_settings( 'page_one', $options );
    return $options;
}


public function generate_scripts() {
    $options = $this->page_one();
    foreach($options as $option) {
        echo $option[0];
    }
}

If you want all methods to be called, you need to add additional code. You could have a property of the class where you push all the arrays as the methods are called, and loop that - but you will still need to call those methods to populate the variable before being able to use it.

Qirel
  • 21,424
  • 7
  • 36
  • 54
  • Let me know if I can help! What exactly are you trying to figure out? – Qirel Jun 12 '17 at 12:18
  • Oh, there was a WordPress function giving a undefined error. Fixed it by placing the return above it instead of under it. Thanks for the help! – J. Doe Jun 12 '17 at 12:46
-1
<?php

class poetry {

    private $options_one;
    private $options_two;
    private $options_three;


    public function __construct() {
        add_action('wp_head', array($this, 'generate_scripts'));
    }

    public function page_one() {
        global $options_one;
        $options_one = array(
            array('content1', 'Content1', 'The Content 1'),
            array('content2', 'Content2', 'The Content 2'),
            array('content3', 'Content3', 'The Content 3'),
        );
    }

    public function page_two() {
        global $options_two;
        $options_two = array(
            array('content4', 'Content4', 'The Content 4'),
            array('content5', 'Content5', 'The Content 5'),
            array('content6', 'Content6', 'The Content 6'),
        );
    }

    public function page_three() {
        global $options_three;
        $options_three = array(
            array('content7', 'Content7', 'The Content 7'),
            array('content8', 'Content8', 'The Content 8'),
            array('content9', 'Content9', 'The Content 9'),
        );
    }

    public function generate_scripts() {
        $this->page_one();
        $this->page_two();
        $this->page_three();
        global $options_one, $options_two, $options_three;
        $options = array_merge($options_one[0], $options_two[0], $options_three[0]);
        foreach ($options as $option) {
            echo $option . "<br/>";
        }
    }

}
Parag Soni
  • 693
  • 7
  • 14
  • No explanation, and just a code-dump. It's not going to be much helpful. Besides, usage of `global` isn't a good idea to begin with.. – Qirel Jun 12 '17 at 11:48
  • Well it is not code dump. I modified it by self, and if your code is good enough you do not need an explanation. – Parag Soni Jun 12 '17 at 11:55
  • That is exactly what it is, a code-dump! There's no text to explain or support it. So you need to study each line to see what changed you made, which isn't a good answer. [How to write a good answer (SO meta)](https://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question). And even so, the approach with having `global` really isn't a good idea, [why you shouldn't use `global`](https://stackoverflow.com/a/1558073/4535200). And if you're going to downvote my answer too, at least write a comment *why* ;-) – Qirel Jun 12 '17 at 12:00