Why are attributes defined in a class independent of the sequential order?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.