1
class Someclass{

    function topFunction()
    {       

        function makeMeGlobal($var)
        {
            global $a, $b;

            $a = "a".$var;
            $b = "b".$var;
        }

        makeMeGlobal(1);    
        echo "$a <br>";
        echo "$b <br>";

        makeMeGlobal(2);    
        echo "$a <br>";
        echo "$b <br>";
    }
}

I am using that test code on codeigniter but nothings happen.

I suppose to print a result like this

a1
b1
a2
b2 

How to handle those functions inside a class?

2 Answers 2

1

You declare globals inside function scope.

Try to declare them at class scope:

class Someclass{

    function topFunction()
    {       

        function makeMeGlobal($var)
        {
            global $a, $b;

            $this->a = "a".$var;
            $this->b = "b".$var;
        }

        makeMeGlobal(1);    
        echo $this->a . "<br>";
        echo $this->b . " <br>";

        makeMeGlobal(2);    
        echo $this->a . "<br>";
        echo $this->b . "<br>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you are creating the global variables inside the function, try creating them in the class scope rather than the function. that should work.

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.