Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP require an explicit reference to "$this" to call member functions?

Tags:

oop

php

That's pretty much it. Scoping in most OO programming languages resolves symbols fine without an explicit reference to the current instance (i.e. '$this' in PHP). Why does PHP require me to precede every call to a member function in the same class with $this?

like image 769
rinogo Avatar asked Aug 08 '26 01:08

rinogo


1 Answers

To resolve the scope of the function call. Consider:

$this->strstr(...

and just

strstr(...

The latter will call PHP's built-in strstr() function which is not what we want to do here.

It's a consequence of adding OOP features to a high level procedural/scripting language.

like image 122
karim79 Avatar answered Aug 10 '26 16:08

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!