Understanding $this Showing the Instance Himself in PHP

Asked 2 years ago, Updated 2 years ago, 390 views

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.

php

2022-09-30 21:56

2 Answers

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!


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.