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
  • 75,446
  • 20
  • 155
  • 173
Ipsita Rout
  • 4,525
  • 3
  • 35
  • 38

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
  • 75,446
  • 20
  • 155
  • 173
dqhendricks
  • 17,761
  • 10
  • 48
  • 82
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
  • 124,572
  • 19
  • 146
  • 171