Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6938747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:31:53+00:00 2026-05-27T12:31:53+00:00

Super easy question. Look at the 2 sample class methods. In the first One

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:31:54+00:00Added an answer on May 27, 2026 at 12:31 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a super easy question but I'm banging my head on it
Super easy, I am sure - but in conditional if/then statements using droid, how
super easy question that i can't find the answer to on eclipse's website.... i
This is probably a super easy question, but I just wanted to make 10000%
Super-beginner easy points ruby question. I'm trying to learn some ruby by programming the
This may be super easy to solve but I can't find the correct google
Now I'm sure this is super easy, I have a function called remove_deposit which
Super-newbie question! I've been looking for a list of all the classes that come
I have a super-simple query in a star schema. One fact, two dimensions. I
First time I post a question, so here it goes. I want to push

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.