Ad

Laravel: Accessing Variable Declared Outside Of A Function

- 1 answer

I want to have a variable that may be used inside and outside of the functions. On PHP manual I found the following example: http://php.net/manual/ro/language.variables.scope.php

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>

Manual says:

The above script will output 3.

But my laravel output for this code (in public show function inside a contreller) is 2.

How to make this work as needed?

Ad

Answer

Try this Code

class TestController extends Controller {

private $search;

public function __construct() {
    $this->search = 1;
}

public function global () {
    echo $this->search;
}
Ad
source: stackoverflow.com
Ad