1

As I continue to try and improve myself as a junior PHP developer, I have started to try break down other peoples work. I find it helps me understand, as well as giving me ideas.

Two things I do not get, in a PHP class, what $this means, and what array($this,'some_function') means when I would expect a function name in it's place.

Many thanks

Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148

2 Answers2

8

Please see the PHP Manual on Classes and Objects. $this refers to the object instance. Also see these questions:

For array($this,'some_function') see the PHP manual on callbacks. In methods and functions that do accept callbacks, like call_user_func, it means call the method some_function on $this. See

Marking this as CW because it's not meant as an answer but the reason for closevoting as duplicate

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • why would we use this syntax, and not $this->some_function()? – Mild Fuzz Sep 24 '10 at 11:07
  • @Mild because `$this->some_function()` would execute the function, while `array($this, 'some_function')` does not and thus allows passing the function around for defered calling. – Gordon Sep 24 '10 at 11:18
1

$this is a class pointer. $this in class give access to class properties and methods.

Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143