I have a question how to call a variable.
In Js, if I write $A = $this->A inside of B(), I could reach $this->A through $A inside of C(); But in PHP seem like this way will not work.
Please advice a way that can read $this-A inside of C() without give function variable.
class X {
function B(){
function C(){
$this->A; // Can not call A here
$A; // No work either
}
$A = $this->A; //
$this->A; // Can call A here
}
private $A;
}
This one here is Global $A
Get variables from the outside, inside a function in PHP
However,if global, the global here means the entire script not within the class.
Thank you very much for your advice.
You cannot define functions within functions, avoid it, it is bad! The function will not be restricted to the scope. Functions are always global.
Although it will work on the first run, the execution of such code will only define the function then (it will be unavailable for calls before), and on the next call will crash with “redefined function error”.
Knowing this, what your code does is define a global function “C” that is actually outside the class, and thus has no access to private properties.