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 959437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:01:36+00:00 2026-05-16T01:01:36+00:00

Why does PHP require you to explicitly write $this ? I would understand if

  • 0

Why does PHP require you to explicitly write $this? I would understand if you had to use $this here:

function foo($bar) {
   $this->bar = $bar;
}

But you must write it explicitly in verbose code that looks like this:

$this->var3 = globalFun($this->var, $this->var2[$this->anotherVar], $this->method());

as opposed to:

$var3 = globaFun($var, $var2[$anotherVar], method());

So what is the point of $this?

Additional Bonus Question:

Why do we have to differentiate static references and instances? Why do we need:

static function getValue() {
   return self::value;
}

Can’t PHP find out at runtime if the variable/method in question is static? Now if I want to change a method from static to non-static, I have to replace all those self:: with $this-> (and vice-versa).

Wouldn’t it be better if we had a $this that behaves like it does in Java?

  • 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-16T01:01:37+00:00Added an answer on May 16, 2026 at 1:01 am

    Okay, so let’s remove the need for writing $this everywhere. Take a look at this situation:

    class Foo {
        public function setBar($value) {
            $bar = $value;
        }
    }
    $foo = new Foo();
    $foo->setBar('some value');
    

    Is $bar a local variable or a member of $foo?

    There has to be some differentiation. They could have allowed declaration of local variables with the var keyword, but that would not have been backwards-compatible and would have been very confusing to people upgrading from older versions of PHP.

    Same thing applies to self::. How does the interpreter know whether the function you wanted to call is global or specific to the class?

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

Sidebar

Related Questions

any time i write this: $this->setTemplate('module/action'); I'm getting this error: The template module/actionSuccess.php does
Here is my code: if (file_exists('config.php')) { require('config.php'); // This is line 38 }
Why does the PHP mail() function require a mail program like sendmail/postfix/etc. for sending
Ok it does work in PHP but if I modify the file so that
I'm creating a drupal template and tried using this <?php require .base_path() . path_to_theme().'/header.php'
I'm currently working on a PHP/MySQL script that does the following, in this order:
I'm working on a large php site that currently does not use any framework.
for some reason I get this error below when trying to use multiple require()
So I am trying to autoload classes in PHP; however, the __autoload() function does
I've had something strange happen lately with php script I use to send emails

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.