One reference book ("Details PHP7 + MySQL Introductory Note" p220)
It is written in as follows.[ ] in <Reference Books>
// [$name is to find local variable with the same name] value is null.
In this case, "local" in "local variable" means
hDo you mean that it is within the scope surrounded by {} below hello()?
それ Or do you mean within the definition of class bounded by {} below class OurList in <Description> below?
I'm a beginner and confused by the term local variable.①どちらWhich one is correct? Or does it fall within the range other than this? If the term "local variable" appears in this book, please explain the scope so that even beginners can understand it.
Thank you for your cooperation.
* Wrong code
public function hello(){
echo "Hello {$name}!";
}
●$this->property name
The above code is incorrect.$name cannot access property $name. [$name looks for local variables with the same name] The value is null.
To access the property $name, you must write "$this->name" using $this pointing to the instance itself.
正しい Correct code
public function hello(){
echo "Hello {$this->name}!";
}
<?php
class OurList {
public$name;
public$age;
public function hello() {
echo "Hello {$this->name}!";
}
public function age() {
echo "{$this->name} is {$this->age} years old this year.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<title> Class List</title>
</head>
<body>
<pre>
<?php
$rika = new OurList();
$taichi = new OurList();
$rika->name="Satokako";
$rika->age=21;
$taichi->name = "Taiichiro";
$taichi->age=19;
print_r($rika);
print_r($taichi);
$rika ->hello();
echo "<br>";
$taichi ->hello();
echo "<br>";
echo "<br>";
$rika->age();
echo "<br>";
$taichi->age();
?>
</pre>
</body>
</html>
OurList Object
(
[name] => Satoko
age = > 21
)
OurList Object
(
[name] => Taiichiro
[age] = >
)
Hello, Satoko!
Hello, Mr. Taiichiro!
Satoko is 21 years old this year.
Yasuichiro is 19 years old this year.
Your answer is
h Within the scope enclosed by {} below hello()
"Local" refers to the scope range of a specific variable and
Local variables refer to variables within the scope of a particular variable.
For example, if you have the following functions:
<?php
$a = "Hello"; // A
functionHelloWorld($a){//B
$a = "World"; // C
echo$a;//D
}
HelloWorld($a);//E
You can see that variables in functions have different scopes.
I think it's confusing because the variable and the properties of the class are treated in the same row.
It's written in a very similar way, but its properties are completely different.
It's good to have the perception that each one is different.
First,
class OurList {
public$name;
public$age;
:
:
}
As the , the member variable n$name が is defined, but access to this variable (referencing or substituting a value) in the class must be <$this->name. と$this refers to the class itself.
So if you want to use the $this->name value in the function,
public function hello(){
echo "Hello {$name}!";
}
It is wrong to write $name as shown in .
The $name in this function can only be used in the function, and the $name in the function is null because it is new here (no value has been substituted yet).
The scope determines where variables can be used.I omit the scope because someone else has already answered it, but local variable is available only in limited scope for the global variable that can be used in all scopes.
($this->name is also a local variable because the scope is only in the class.)
"So, in this case, does the ""local"" of the ""local variable"" in your question mean と?"②"Do you mean ""strong>"" is the answer to ""strong>"".
"I'm sorry if it's hard to understand!
593 GDB gets version error when attempting to debug with the Presense SDK (IDE)
594 Uncaught (inpromise) Error on Electron: An object could not be cloned
575 PHP ssh2_scp_send fails to send files as intended
567 Who developed the "avformat-59.dll" that comes with FFmpeg?
566 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.