lock
lock

Reputation: 739

global variable in controller laravel 5.3

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;
    }
}

Upvotes: 6

Views: 67167

Answers (2)

Ankit Sharma
Ankit Sharma

Reputation: 1

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();
    }

Upvotes: 0

Sovon
Sovon

Reputation: 1824

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;
    }
}

Upvotes: 42

Related Questions