I am not unfamiliar with this parameter in JavaScript. But in Joomla's PHP files, I often come across $this that seems coming from nowhere(Not in an object). To clarify some basic notions, here are some questions:

  1. In JavaScript, generally, if this is not inside an object, then it is referring to the global object, which in a browser environment is window. I understand that in PHP there is no such thing, but when it comes to what $this is referring to, is there any similar thing?

  2. I think in most cases, for those $this not in an object, they actually reside in an object that is defined in another file, which 'require's current file, am I right? If yes, how can I find out where that file is, or what object $this resides in? Using the extension "J!dump" I can actually see what $this is, but I still want to know how to find out the parent object.

  • Take a look at this. Think it will answer both your questions
    – Lodder
    Oct 13 '15 at 16:22
  • Actually I read this before, but I still don't get it. The answer is just about the basic concept of this parameter and in all the examples. $this is explicitly in an object.
    – shenkwen
    Oct 13 '15 at 17:31
  • I think in this case it would be some subclass of JView or JViewLegacy. The file you linked looks like a layout file, which would be called from inside a view. Including the filename and folder name actually helps with Joomla... Oct 13 '15 at 23:55
  • This doesn't answer your question exactly, but if you used a tool like PHPSTORM you would be able to inspect the $this value as the program executed. Other programs that use xdebug should be able to give you with the same sort of information.
    – TryHarder
    May 16 '18 at 2:18

To answer 1, no there is no global PHP $this variable that correlates with JavaScripts this varaible. This can be easily tested with a simple PHP file:

echo $this;

which will give you an error.

To answer 2, $this is always inside a class. What I think you are trying to understand is class inheritance. So say you are looking at the file cat.php:

class Cat extends Animal {

   protected $meow = 'meeoooowww';

   public function Meow() {
     echo $this->meow . ' I am ' . $this->age . ' years old';


and somewhere else you have a file animal.php:

class Animal {

   protected $age = '10';

   public function run() {
     echo "I'm running";

Finally you create a new cat in some other file:

$tinkles = new Cat();
echo "<br>";

You are going to see this outputted:

I'm running
meeoooowww I am 10 years old

Note how the cat has inherited the function (run) and propertie (age) from Animal by using "extends Animal" as part of the class declaration.

PHP then has several methods for understanding what the class is, In the next snippet we will add an introspect() function, using those methods:

class Cat extends Animal {

   protected $meow = 'meeoooowww';

   public function Meow() {
     echo $this->meow;

   public function introspect() {
     echo "My class is ". get_class($this) . "<br>";
     echo "My ancestor class is " . get_parent_class($this);


So now:

$tinkles = new Cat();

will produce:

My class is Cat
My ancestor class is Animal

Finding out where those files resides can require some detective skills if you are not familiar with the framework.

You would benefit from setting up xdebug for example to be able to step through what happens when your code is executed.

Also a good IDE such as PHPStorm will allow you to click on the class name, for example "Animal" in "class Cat extends Animal" to open up that file directly.

  • Thanks for pointing out get_class and get_parent_class. The PHP file I am looking at is here:goo.gl/nPtF2L, the first $this appers in line 15, it is not in any object in this file. This gives me a hard time. So if you want to find out what $this is here, generally what would you do? Will you var_dump it or use get_class/get_parent_class, or some other ways?
    – shenkwen
    Oct 13 '15 at 20:42
  • That looks like a view template file. In which case $this is referring to the view class that called the template file. So say the file is components/com_foo/views/home/tmpl/default.php $this would generally refer to components/com_foo/views/hone/tmpl/view.html.php Oct 14 '15 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.