何时在PHP中使用$ this-> property代替$ property [英] When to use $this->property instead of $property in PHP
问题描述
超级简单的问题.查看2个示例类方法.
Super easy question. Look at the 2 sample class methods.
在第一个中,我传入变量/属性调用$params
,然后执行$this->params
In the first One I pass in a variable/property call $params
I then do $this->params
我的问题是,是否真的需要这样做,我通常是这样进行的,但我注意到,在第二个示例中,仅调用$params
而不设置$this
即可使用它.
My question is, is it really needed, I generally do it this way but I have noticed that it will work in the second example with just calling $params
without setting $this
to it.
所以我的理论是...如果您需要使用该类中的其他方法访问该属性,则必须将其设置为$this->params
,如果仅在该属性中使用该属性,则只能使用$params
.同样的方法.
So my theory is this... You have to set it like $this->params
if you need to access that property in a different method in that class and you can use just $params
if you are only using that property in that same method it is in already.
有人可以阐明这一点,并解释一下我的理论是否正确,或者我是否相距遥远.我想知道这样做的原因,这样我就知道什么时候该使用每种方法或全部使用一种或另一种方法了.时间,谢谢你
Could somebody shed some light on this and explain if my theory is correct or if I am way off I would like to know the reasoning for this so I will know when do do each method or to do one or the other all the time, thanks you
class TestClass{
public function TestFunc($params){
$this->params = $params;
echo 'testing this something'. $this->params;
}
}
没有定义变量
class TestClass2{
public function TestFunc2($params){
echo 'testing this something'. $params;
}
}
推荐答案
访问类变量时使用$this
.
当访问实际上是函数中参数的变量时,不需要使用$this
关键字.实际上,访问名为$ params的函数参数时,不应使用$ this关键字...
When accessing a variable which is actually a parameter in a function, there's no need to utilize the $this
keyword.. Actually, to access the function parameter named $params, you should not use the $this keyword...
在您的示例中:
class TestClass{
public function TestFunc($params){
$this->params = $params;
echo 'testing this something'. $this->params;
}
}
TestFunc($params){
中的
$params
是函数TestFunc
的参数/自变量,因此您无需使用$this
.实际上,要访问参数的值,一定不要使用$this
-现在,当您使用$this->params = $params = $params;
中的$this->params
时,实际上是在设置与 parameter $params
转换为名为 $params
的新类级变量(因为您没有在示例代码中的任何地方声明它)
$params
from TestFunc($params){
is a parameter/argument of the function TestFunc
and so you don't need to use $this
. In fact, to access the parameter's value, you must not use $this
-- Now when you used $this->params
from $this->params = $params = $params;
, you are actually setting a value equivalent to that of the parameter $params
to a NEW class-level variable named also $params
(since you didn't declare it anywhere in your sample code)
[edit]根据评论:
[edit] based on comment:
看这个例子:
class TestClass{
public function TestFunc($params){
$this->params = $params;
# ^ you are setting a new class-level variable $params
# with the value passed to the function TestFunc
# also named $params
echo 'testing this something'. $this->params;
}
public function EchoParameterFromFunction_TestFunc() {
echo "\n\$this->params: " . $this->params . "\n";
# now you are echo-ing the class-level variable named $params
# from which its value was taken from the parameter passed
# to function TestFunc
}
}
$tc = new TestClass();
$tc->EchoParameterFromFunction_TestFunc(); # error: undefined property TestClass::$params
$tc->TestFunc('TestFuncParam');
$tc->EchoParameterFromFunction_TestFunc(); # should echo: $this->params: TestFuncParam
在没有先调用TestFunc
的情况下调用EchoParameterFromFunction_TestFunc
时的错误是由于未声明/设置名为$params
的类级变量/属性-您在TestFunc
内部进行了设置除非您调用TestFunc
,否则不会被设置.对其进行正确设置以使任何人都可以立即访问它,是:
The error when you called EchoParameterFromFunction_TestFunc
without first calling TestFunc
is a result of not declaring/setting the class-level variable/property named $params
--you set this up inside TestFunc
, which means it doesn't get set unless you call TestFunc
. To set it right so that anyone can immediately access it is to:
class TestClass{
# declare (and set if you like)
public /*or private or protected*/ $params; // = ''; or create a construct...
public function __construct(){
# set (and first declare if you like)
$this->params = 'default value';
}
...
...
...
正如 @liquorvicar 所述,我也完全同意:无论是否要使用它们,都应始终声明所有类级别的属性/变量.举例来说,原因是您不想访问尚未设置的变量.请参阅上面的示例,该示例引发了错误undefined property TestClass::$params
.
As @liquorvicar mentioned, which I also totally agree with is that you should always declare all your class-level properties/variables, regardless of whether or not you will use them. Reason being and as an example is that you don't want to access a variable that hasn't been set. See my example above which threw the error undefined property TestClass::$params
..
感谢@liquorvicar提醒我..
Thanks to @liquorvicar for reminding me..
这篇关于何时在PHP中使用$ this-> property代替$ property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!