When to use $this->property instead of $property in PHP

前端 未结 5 930
轮回少年
轮回少年 2020-12-18 09:32

Super easy question. Look at the 2 sample class methods.

In the first One I pass in a variable/property call $params I then do $this->params

5条回答
  •  离开以前
    2020-12-18 10:03

    Use $this when accessing class variables.

    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...

    In your example:

    class TestClass{
    
        public function TestFunc($params){
           $this->params = $params;
    
           echo 'testing this something'. $this->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] based on comment:

    Look at this example:

    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
    

    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';
        }
    ...
    ...
    ...
    

    [edit : additional]

    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..

    Thanks to @liquorvicar for reminding me..

提交回复
热议问题