Call Variable from a function to another function within PHP class

930

i use this class below i want use the variables array or property array ($g_types) in first_function how i can do this?

class My_Class {
    function first_function() {
        //HOW I CAN USE THE $g_types array Variable values in this function?
    }
    function second_function() {
        $g_types = array(
            'Person Widget'    => 'g_person',
            'Page Widget'      => 'g_page',
            'Community Widget' => 'g_community'
        );
    }

}
222

Answer

Solution:

One possible way to archive this, is by using a class-variable.

class My_Class {
    private $g_types;
    function first_function() {
        $gperson = $this->g_types['g_person'];
    }
    function second_function() {
        $this->g_types = array(
            'Person Widget'    => 'g_person',
            'Page Widget'      => 'g_page',
            'Community Widget' => 'g_community'
        );
    }
}
493

Answer

Solution:

Well you can return $g_types in second function and use it in first function.

function first_function()
{
$returnedValue = second_function();
}

But i suggest you to read about scope of the variables in php so that you won't encounter this problem anymore.

People are also looking for solutions to the problem: php - Crowd RESTful API cannot connect

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.