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

when to use this symbol
when to use this punctuation
when we use that
when to use this sign
when to use this c#
difference between that and those
use of this and it in hindi
these in a sentence

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

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.

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;

without defining variables

class TestClass2{

    public function TestFunc2($params){
       echo 'testing this something'. $params;

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

This, that, these, those - English Grammar Today, In writing fiction, 'this' and 'that' can be used very effectively to connote different things. They are functionally equivalent, but the closeness of 'this' and the  Use definition is - to put into action or service : avail oneself of : employ. How to use use in a sentence. Synonym Discussion of use.

I hope this will help clear things up:

class Person {

   private $name;
   private $age;

   public function __construct($name, $age){
     // save func params to instance vars
     $this->name = $name;
     $this->age = $age;

   public function say_hello(){ // look, no func params!
     echo "My name is {$this->name} and I am {$this->age} years old!";

   public function celebrate_birthday(){ // no params again
     echo "Party time!";
     $this->age += 1; // update instance var
     $this->drink_beer();  // call instance method

   public function drink_beer(){
     echo "Beer is good!";

$p = new Person("Sample", "20");
$p->say_hello();          // My name is Sample and I am 20 years old!
$p->celebrate_birthday(); // Party time! Beer is good!
$p->say_hello();          // My name is Sample and I am 21 years old!

When to use "this" or "that"?, Generally speaking, we use this/these to refer to people and things, situations and experiences that are close to the speaker or very close in time. We use that/those to refer to people and things, situations and experiences that are more distant, either in time or physically. Writers often use this when a point or idea is to become an important part of the discussion that follows: More and more people are discovering that Tai Chi is one of the most valuable forms of exercise. This has led to a big demand for classes. (This refers back to a whole sentence.)

In general you can use specific variable when they're in the context ("scope") of your object.

In OOP programming, $this will almost always be used to access a class variable or other class method.

class MyClass{
     private $myNum;

     public function myFunc(){
          echo 'My number is '.$this->myNum; // Outputting the class variable "myNum"

     public function go($myNumber){
          $this->myNum  = $myNumber; // Will set the class variable "$myNum" to the value of "$myNumbe" - a parameter fo the "go" function.
          $this->myFunc(); // Will call the function "myFunc()" on the current object


this, that - these, those - English Grammar, This is used to describe a singular countable noun and these is used with plural countable nouns. A countable noun has a plural form such as books, girls, boys, How to Use a Semicolon Correctly. The most common use of the semicolon is to join two independent clauses without using a conjunction like and. Do you use a capital letter after a semicolon? The general answer is no. A semicolon should be followed by a capital letter only if the word is a proper noun or an acronym.

You have already answered your question yourself:

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

Difference Between THIS and THESE, When the noun is omitted after this and these, they become pronouns (i.e. turn this off when you leave). Demonstratives are words we use to indicate nouns in a​  Define use. use synonyms, use pronunciation, use translation, English dictionary definition of use. v. used , us·ing , us·es v. tr. 1. To put into service or employ for a purpose: I used a whisk to beat the eggs.

"these" vs "this" - Grammar Guide, This vs. That This and that are singular. This indicates something physically nearby. It may also refer to something symbolically or emotionally "close." That. Use definition, to employ for some purpose; put into service; make use of: to use a knife. See more.

This and That, These and Those, Than and Then, You use “these” when there are plural objects; you use “this” when you are modifying a singular object. “These books,” “this couch.” These and this are  50 synonyms of use from the Merriam-Webster Thesaurus, plus 65 related words, definitions, and antonyms. Find another word for use. Use: to put into action or service.

How to know when to use 'these' and 'this' in a sentence, http://www.engvid.com/ This or that or these or those? Not sure which word to use at times? I Duration: 7:30 Posted: Jul 4, 2011 Synonyms for use at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for use.

Basic English Grammar - THIS, THAT, THESE, THOSE, If I use commas to separate these items, my sentence looks like this: I bought shiny, ripe apples, small, sweet, juicy grapes, and firm pears. That middle part is a bit  People use which and that every day. Just because these words are common doesn’t mean they’re easy to use. In particular, clauses cause a lot of confusion, but there’s an easy way to remember which one to choose.