I $this->name
ve $this->$name
arasındaki fark nedir merak ediyorum? Ayrıca $this
kesinlikle this ismini vermek ya da herhangi bir şey olabilir var mı?
$this
ayrılmış bir değişken adı ve başka bir şey için kullanılamaz. Özellikle Eğer değişken nesne atanır ne olacağını bilmiyorum, çünkü $this
kullanmak zorunda Lütfen sizin şu anda çalışıyoruz nesneye noktaları size.
$this->name
geçerli sınıfın değişkeni ifade eder name
$this->$name
değeri $name
ne olursa olsun sınıf değişkeni ifade eder. Böylece
$name = "name";
echo $this->$name; // echos the value of $this->name.
$name = "test";
echo $this->$name; // echos the value of $this->test
Bu sen onu kullandığınız sınıf mevcut örneğine işaret PHP kullanılan ayrılmış bir isimdir $ (quoting) em>:
The pseudo-variable
$this
is available when a method is called from within an object context.$this
is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
When using $this->name
, you are accessing the property with the name name
of the current object.
When using $this->$name
, $name is determined before accessing the property -- which means you'll access the property which name is contained in the $name
local variable.
Örneğin, bu kod bölümü ile:
$name = 'abc';
echo $this->$name;
Eğer yazmış gibi aslında, abc özelliğinin içeriğini echo:
echo $this->abc;
Bunu yaparken, kullandığınız variable variables (quoting) edilir:
Class properties may also be accessed using variable property names.
The variable property name will be resolved within the scope from which the call is made.
For instance, if you have an expression such as$foo->$bar
, then the local scope will be examined for$bar
and its value will be used as the name of the property of$foo
.
This is also true if $bar is an array access.