Home > Backend Development > PHP Tutorial > Why is the order of defining attributes in a class not affected?

Why is the order of defining attributes in a class not affected?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:21:55
Original
1137 people have browsed it

Why is defining a variable in a normal function different from defining a property in a class? In the a() function, variables must be defined in front. If $a =1 in the a() function is placed after return, an error will be reported. Why is there no error in the class? Although big hands may think it’s childish, I don’t understand this question

<code>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;</code>
Copy after login
Copy after login

Reply content:

Why is defining a variable in a normal function different from defining a property in a class? In the a() function, variables must be defined in front. If $a =1 in the a() function is placed after return, an error will be reported. Why is there no error in the class? Although big hands may think it’s childish, I don’t understand this question

<code>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;</code>
Copy after login
Copy after login

Because classes are compiled first and then executed, while process-oriented execution is streaming.
This is generally the case. This is what I learned from JS.

The most basic explanation is $a=new aa();At this time, the attribute $name has been assigned a value, but function bb() has not been executed yet.
In fact, object management is very complicated. To put it simply, no matter how you write it, it has been preprocessed at runtime. All properties are allocated memory when instantiated and then constructed.

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template