1

What is the difference between accessing a property from the class through $this or through new operator or through scope resolution operator in PHP?

$this-> vs -> vs :: in PHP

kapa
  • 77,694
  • 21
  • 158
  • 175
Ipsita Rout
  • 4,945
  • 4
  • 36
  • 40

2 Answers2

2

$this-> can be used from inside a class when referencing itself.

$object-> is used from outside the class when referencing a specific object.

$class_name:: is used when referencing a static property or method of a specific class.

kapa
  • 77,694
  • 21
  • 158
  • 175
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

The differen between

$object->property;
Class::property;

is, that the first one access a object property, whereas the second one access a (static) class property. I really dont know, what you mean by "through new operator", because via new no property is accessable in any way, because new just creates a new object instance of a class. However, $this->property is exactly the same, as the first example above, but $this is only valid inside an object method and always references the object itself.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173