0

I have following code in my class called lang.php:

<?php $GLOBALS['app_list_strings']['country']=array (
.....
)


    ...
?>

These array is not defined inside any function and class. I need to get these array values in other controller class. (For example, AdminController.php). How can I access these array values?($GLOBALS['app_list_strings']['country'])

5
  • Got an example of a class you want to get this into? Commented Feb 23, 2017 at 4:37
  • Are you using any framework ? Commented Feb 23, 2017 at 5:13
  • I am using yii 1 framework Commented Feb 23, 2017 at 5:15
  • I highly recommend not using globals just for best practices. If you want, I could write you an answer in normal PHP. I am not sure if yii is any different from the base in the case that I can give. Would you like that? Commented Feb 23, 2017 at 5:45
  • I will be very greatfull, if you help me Commented Feb 23, 2017 at 5:47

1 Answer 1

1

You can just inject your other class into your AdminController class and set up a get method to fetch the array(s) you need. Presumably you have your class set up like so (obviously there would be more script):

class Lang
    {
        public function someMethod()
            {
                $array =  array(
                    'app_list_strings'=>array(
                        'country'=>array(
                            'k1'=>'val1',
                            'k2'=>'v2'
                        )
                    )
                );
            }
    }

If you add a private parameter and a method you can extract that array:

class Lang
    {
        # Create parameter
        private $array;
        # Whatever method contains the array
        public function someMethod()
            {
                # use $this here
                $this->array = array(
                    'app_list_strings'=>array(
                        'country'=>array(
                            'k1'=>'val1',
                            'k2'=>'v2'
                        )
                    )
                );
                # I am just returning self for sake of demonstration.
                return $this;
            }
        # Returns the array
        public function getArray()
            {
                return $this->array;
            }
    }

class AdminController
    {
        # Inject your other class
        public function whateverMethod(Lang $lang)
            {
                # Retrieve array from getArray() method
                print_r($lang->someMethod()->getArray());
            }
    }

To use:

<?php
$AdminController = new AdminController();
$AdminController-> whateverMethod(new Lang());

To get the array just in general:

<?php
$Lang = new Lang();
print_r($Lang->someMethod()->getArray());

If the classes are far removed from each other, in that they are called from different areas of your script and they can not be injected like demonstrated, you can change private $array to private static $array and assign self::$array = array(...etc. then return self::$array. Because it's static it will persist through the script. Last way would be to save to $_SESSION, but that may not be the most desirable solution.

Sign up to request clarification or add additional context in comments.

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.