How to access variables from a previous scope in PHP?

1

First, inside a class I have a method, in this method I have a parameter (for now) and declare some variables in its scope. Therefore, I also declare functions within the scope of the method, and as everyone knows, every function has its own scope.

The problem is that in the scope of the functions that I stated in the scope of the method the variables that I declared previously are undefined, and therefore I can not access them. How can I get around this? I need to access the variables of the previous scope in the scope of these functions.

This is a part of the function I have in my class, that's where I'm having problems.

public static function parse($buf) {
    $i = 0; $chr; $cod;

    // $i, $chr, $cod e $buf
    // são todas invisíveis no escopo das funções abaixo.

    /* Iterate each char in $buf. */
    function itchr($c) {
        for (; $chr = $buf[$i]; ++$i) {
            $cod = getchrcode($chr);
            if ($c() === false)
                break;
        }
    }

    /* Consume $buf string until $c returns truth value. */
    function consumetil($c) {
        $result = (object) array(
            "matched" => false,
            "value" => ""
        );

        for (; $chr = $buf[$i]; ++$i) {
            $cod = getchrcode($chr);
            if ($c()) {
                $result->matched = true;
                break;
            } else $result->value .= $chr;
        }

        return $result;
    }

    /* Consume a little identifier in $buf */
    function consumeid() {
        return consumetil(function() {
            return !isIdentifierPart($cod);
        });
    }

}
    
asked by anonymous 01.10.2016 / 13:58

1 answer

1

Inherit the parent scope variables with anonymous roles ( closures ) , referencing by command use .

Example:

$message = "hi !!!";
$example = function () use ($message) {
    var_dump($message);
};
$example();

I would make it simpler, would place the local variables as private static and would access it with self :

Example:

class g 
{
    private static $message = "Bom dia";
    public static function aa()
    {
        self::$message .= " a nós todos";
        return self::$message;
    }
    public static function getMessage()
    {
        return self::$message;  
    }
}

echo g::aa(); //Bom dia a nós todos
echo '<br>';
echo g::getMessage(); //Bom dia a nós todos

Functional Example

public static function parse($buf) {
    private static $i = 0; 
    private static $chr; 
    private static $cod;

    // $i, $chr, $cod e $buf
    // são todas invisíveis no escopo das funções abaixo.

    /* Iterate each char in $buf. */
    function itchr($c) {
        for (; self::$chr = $buf[self::$i]; ++$i) {
            self::$cod = getchrcode(self::$chr);
            if ($c() === false)
                break;
        }
    }

    /* Consume $buf string until $c returns truth value. */
    function consumetil($c) {
        $result = (object) array(
            "matched" => false,
            "value" => ""
        );

        for (; $chr = $buf[self::$i]; ++self::$i) {
            self::$cod = getchrcode(self::$chr);
            if ($c()) {
                $result->matched = true;
                break;
            } else $result->value .= self::$chr;
        }

        return $result;
    }

    /* Consume a little identifier in $buf */
    function consumeid() {
        return consumetil(function() {
            return !isIdentifierPart(self::$cod);
        });
    }

}
    
01.10.2016 / 15:00