Why is the definition of a variable in a common function different from that in a class? In function a (), variables must be defined first. if $ a1 in function a () is put after return, an error is reported. why is no error reported in the class? Although it may seem naive, why do I define a variable in a common function and an attribute in a class? In function a (), variables must be defined first. if $ a = 1 in function a () is put after return, an error is reported. why is no error reported in the class? Although it may seem naive, I don't understand this problem.
function a(){ return $a; $a=1;}echo(a());----------------------------------------------class aa{ function bb(){ return $this->name; } public $name=4; }$a=new aa();$b=$a->bb(); echo $b;
Reply content:
Why is the definition of a variable in a common function different from that in a class? In function a (), variables must be defined first. if $ a = 1 in function a () is put after return, an error is reported. why is no error reported in the class? Although it may seem naive, I don't understand this problem.
function a(){ return $a; $a=1;}echo(a());----------------------------------------------class aa{ function bb(){ return $this->name; } public $name=4; }$a=new aa();$b=$a->bb(); echo $b;
Because the class is compiled first and then executed, and the process-oriented is stream execution.
This is usually the case, which I learned from JS.
The initial explanation is$a=new aa();
The $ name attribute has been assigned a value, but function bb () has not been executed yet.
In fact, object management is very complicated. Simply put, no matter how you write it, it has been preprocessed during running. all attributes are allocated memory before instantiation, then execute the construction.