6

How can definition a global variable for use in all function controller

class TestController extends Controller
{
    private $x;

    public function index()
    {
        $this->$x ='22';
    }            

    public function send_message()
    {
        echo $this->$x;
    }
}

2 Answers 2

42

Write $this->x rather than $this->$x

class TestController extends Controller
{
    private $x;

    public function index()
    {
        $this->x ='22';
    }

    public function send_message()
    {
        echo $this->x;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to change the value of $this->x?
@Vincent Yes you can. Like this: $this->x = 50;
0

If you want to make global variable in controller, the following code will work definitely:

private $x = 22;
    public function index()
    {
    print_r($this->x);
    die();
    }

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.