Dynamically Access PHP Object Properties with $this

19 Mar 2022

"What does the $this variable mean in PHP?"
"What does $this mean in PHP."
and other similar questions increasingly appearing on the Internet.
The Stackoverflow group is also bombarded with questions about this variable.
We could tell you how the variable $this works in PHP. Let's find out everything related to dynamic PHP access object property with $this and break it down with examples.
If suddenly you already know everything, but don't know how to do something, then our Drupal services are ready to help you.

What does $this mean?

Chances are you've seen the $this variable in PHP many times and probably have no idea what it's used for. Let’s give it a little review today. We will try to explain to you clearly and simply.
So in Object-Oriented PHP, the $this variable is a PHP object pseudo-variable (aka a reserved keyword) that refers to the calling object.
In practice, it looks like this:
class Person {
public $name;
  function __construct( $name ) {
    $this->name = $name;

$jack = new Person('Jack');
echo $jack->name;

What does $this PHP object variable do, and where to use it?

$this refers to the methods and PHP properties of the current class instance. It helps you to access a class file internally. For example, you can use $this to represent the members of a specific class. That is $this variable is better for the current object and non-static members. It may lead to an error if to use it in another way.

How to use $this? [explained with example code]

Let's take a look at the first case:

$nid = $row->{$this->aliases['nid']};

Looking at this code, we can see that we want to retrieve the $nid value from the $row object. There is one problem, we did not know the property's name on $row.

Let's look at one more case:

$nid = $row->my_nid;

This code snippet is a bit simpler but similar to the previous example. What is it all for? You will understand it in a minute.
Imagine that you need to access the $row->my_nid property. The average programmer would solve this problem like this: $nid = $row->my_nid;.Or it can be done like so:

$nid_property = 'my_nid';
$nid = $row->{$nid_property};

When using the ->{$nid_property} notation, evaluate the $nid_property variable and replace the value with the name of the property to access. PHP will eventually interpret this as $row->my_nid.

The biggest and best thing here is to provide any logic for $nid_property. Precisely this logic determines what the PHP property name should be.
To clarify all of the above, let's look at the example:

$nid = $row->{$this->aliases['nid']};

Let's say you want to add a new View using the Views UI. You can add the same field as many times as necessary to do this. Or, you have an alternative solution. Try to add the field node.title and after add the node.title field of different nodes pulled using reference.

The Views module works in such a way: adding a field to a View creates a different and unique name for each field and uses it in the corresponding SQL query.
MySQL does not accept the usage of two columns with identical names. So that these names do not repeat, you need to come up with aliases. You must access individual values in the PHP representation number of the row returned from MySQL.

Views do not produce this, but our Drupal 8 development team will use it to show how these types of problems can be solved. Again, fewer words, more action. Let's illustrate everything with an example.

SELECT node1.nid AS node_1_nid, node2.nid AS node_2_nid FROM node node1 LEFT JOIN node node2 ON node2.nid = node1.some_other_field;

Views will execute your query, and the results will be stored in the $row variable. Keep in mind that all properties will be equal to the aliases used in the query that we talked about earlier. So we have two properties: node_1_id and node_2_nid. And they are both generated without being interrupted by Views. They can not just be called "nid."

Therefore, if you need to find the value of some NID, you must write the alias' name. These names are stored by Views and are in the $this->aliases property.
As a result, to get the value of the NID you should write code along these lines:

$nid = $row->{$this->aliases['nid']};

This command will start searching the $this->aliases array to find the value of the nid key.
After you get something like this:

$nid = $string->node_1_id;

Use PHP object variable $this to access a class file internally!

It is definitely worth paying attention to access PHP object properties with $this dynamically. Especially if you want to be one step ahead of other programmers and get better and faster results. The $this variable in PHP is a black horse. And only those who know how to use $this in PHP correctly get more.

The main things you should remember about PHP object variable $this:

  • it helps you to access the class file inside
  • PHP's $this property cannot be used in static functions
  • it requires an instance of an object
  • $this indicates the current context of Class

We hope that in our blog you have found answers to all questions about the property of the PHP access object. Drupal web design company is ready to bring all your ideas to life.
Have additional questions that we haven't covered here? Don't hesitate to ask!
Stay up-to-date and use $this variable. Best wishes, your Golems' team.