3

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?

Community
  • 1
  • 1
Mahoor13
  • 5,297
  • 5
  • 23
  • 24
  • 1
    I know the diffrence of static calling and instance calling of methods. $this:: and $this-> acts the same but self:: and this:: are diffrent. – Mahoor13 Nov 12 '12 at 15:26
  • http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Gordon Nov 12 '12 at 22:25
  • 1
    I don't consider this question a duplicate. It is specifically focused on `$this::`. Other links shown do not have clear discussions of `$this::`. (For which the simple answer is "don't use that syntax - it appears to be an accidental or non-obvious consequence of PHP's definitions for `$this` and `::`. Instead, stick to `$this->`, `self::`, or `static::`, depending on the meaning you intend.) – ToolmakerSteve Oct 07 '19 at 09:30

2 Answers2

6

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.

Colin M
  • 13,010
  • 3
  • 38
  • 58
  • 1
    Thanks for your answer but I want the diffrence of $this:: and $this-> in my example both of them return the same value (self:: returns 1) and I can not use class1::bar(); out of a class in this example – Mahoor13 Nov 12 '12 at 14:39
  • 1
    I get it, see my edit to the very end. I don't believe it's supported functionality, I think it's just a side effect. – Colin M Nov 12 '12 at 15:14
  • "base class" is not correct terminology. In OO, "base" refers to a parent or ancestor class. Instead say that "static::" refers to *"the class that the object is a (direct) instance of"*. Or similar phrasing. As contrasted with "self::". which refers to *"the class in which the *code* (that says 'self::') is defined"*. [BTW, this is terrible terminology on PHP's part; "static::" is doing something "dynamic" (late binding), and "self::" is doing something "static" (avoiding vtable). Its rather backwards, and not consistent with any other language, or with how one would describe it in English.] – ToolmakerSteve Oct 07 '19 at 09:36
5

the $this-> is called from instance , so you have to have an instance of the object to use this call.

but $this:: is a static method inside the class you can call it without instance so you do not need to alloc an instance of the class to call it like

myclass::doFoo();
public static function doFoo(){ .... }

and the do function should be static function to call it like this or the php will give error in the newer versions

Omar Freewan
  • 2,678
  • 4
  • 25
  • 49
  • I think $this:: is not static because calling class1::bar(); out of a class throws this error: Fatal error: Class name must be a valid object or a string – Mahoor13 Nov 12 '12 at 14:29
  • yes i agree with you the $this pointer is an instance of the class so he can access the static and non static function also this applies to the instances but by instance not like this call $this::doFoo(); – Omar Freewan Nov 12 '12 at 14:32
  • you should call it by instance $this->doFoo(); or get_class($this)::doFoo(); – Omar Freewan Nov 12 '12 at 14:36