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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:16:17+00:00 2026-06-14T05:16:17+00:00

Possible Duplicate: PHP: self vs. $this I’ve found that I can call class methods

  • 0

Possible Duplicate:
PHP: self vs. $this

I’ve found that I can call class methods by $this:: prefix. example:

class class1 {
    public function foo()
    {
        echo "1";
    }
    public function bar()
    {
        $this::foo();
        //in this example it acts like $this->foo() and displays "2"
        //using self::foo() displays "1"
    }
}

class class2 {
    public function foo()
    {
        echo "2";
    }
    public function bar()
    {
        class1::bar();
    }
}

$obj = new class2();
$obj->bar(); // displays "2"
class1::bar(); // Fatal error

I want to know whats the diffrence of calling method with $this-> and $this:: prefixes.

ps:
There is a page about diffrence of $this->foo() and self::foo() in this link:
When to use self over $this?

  • 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-06-14T05:16:18+00:00Added an answer on June 14, 2026 at 5:16 am

    The answer that you linked tells you exactly what you’re looking for ;).

    Basically, there are two concepts that some people have a tough time differentiating in programming languages: objects and classes.

    A class is abstract. It defines a structure of an object. The properties and methods that an object would contain, if an object were built from that class. Creating an object is what you do when you call new class1(). This is instructing PHP to create a new object with all of the properties and methods on the class1 class.

    The important thing to note about creating an object is that it also has its own scope. This is really where $this vs static:: (note: do not use self:: or $this::, please use static:: (more on this later)) come in to play. Using $this is instructing PHP to access the properties and methods of your current object. Using static:: is instructing PHP to access the properties and methods of the base class that your object is constructed from.

    Here’s an example:

    class MyClass {
        public $greeting;
        public static $name;
    
        public greet() {
            print $this->greeting . " " . static::$name;
        }
    }
    
    $instance1 = new MyClass();
    $instance1->greeting = 'hello';
    
    $instance2 = new MyClass();
    $instance2->greeting = 'hi';
    
    MyClass::$name = 'Mahoor13';
    
    $instance1->greet();
    $instance2->greet();
    

    I didn’t test the above, but you should get:

    hello Mahoor13
    hi Mahoor13

    That should give to a general idea of the difference between setting a class property and setting an instance property. Let me know if you need additional help.

    Edit

    $this:: appears to just be a side effect of the way that PHP handles scopes. I would not consider it valid, and I wouldn’t use it. It doesn’t appear to be supported in any way.

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

Sidebar

Related Questions

Possible Duplicate: Why does PHP 5.2+ disallow abstract static class methods? Why can't you
Possible Duplicate: WAMP Stack PHP Fatal error: Class ‘SoapClient’ not found I downloaded a
Possible Duplicate: PHP: Require() and Class Hierarchies Is this bad practice? Is there any
Possible Duplicate: PHP: self vs. $this What does $this-> mean in CakePHP? Please answer
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: PHP: self vs. $this it is from php manual, please let me
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: What exactly is late-static binding in PHP? In this example, PHP will
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: php false place in condition I have noticed that a lot of

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.