Programming: for what in the RNR language the pseudo-variable of $this is necessary and how to use it?

Programming: for what in the RNR language the pseudo-variable of $this is necessary and how to use it?

When learning an interpreted language of PHP the beginning web programmers face such concept as the pseudo-variable of $this. Its assignment and rules of use in the code strongly differ from all other variables therefore it is worth stopping on this question in detail.

Classes and objects

The Object-oriented Programming (OOP) which is used in PHP starting with the 5th version gives to the programmer an opportunity to create any number of copies of the same class, called objects; at the same time each educated copy receives the name. The object can accept the data called arguments, process them by means of functions and give result. Any function of a class can contact its properties not on a straight line but only through a design an object-> property therefore there is a question: how to write such universal code which will allow to work with data to any educated object irrespective of his name? Let's review the example displayed in figure 1.

This code announces the class Mathematics having the variable (property) digit and two functions (method), one of which is a designer, i.e. is automatically started during creation of a new object. The problem of function designer is to assign to digit property the data obtained by $dig argument during creation of an object. The getDigit method () by his call returns value of digit property.

Further we will consider lines 12 and 13. In them two new copies of the class Mathematics, one of which receives number 5 as argument, and another - 7 are created. These values are assigned by function by the designer to the variable (property) private of $digit available only in a class. Each created object is assigned to variable $a and $bсоответственно (if to speak more precisely, then these variables receive only links to the specified objects, but it does not matter at present). Now it is possible to receive values of digit property a simple method call of getDigit (line 15 and 16).

Assigning pseudo-variable of $this

Pay attention: we have two different objects having absolutely identical methods. In what way does the method obtain information on that what data of an object need to be processed if its code was registered even before creation of this object?

And here the pseudo-variable of $this comes to the rescue. Its name can be translated from English as "this", i.e. $this indicates (is the link) that object in which it is. As a result line 5 for $a can be read as "assign to digitobjekta property of $a argument value of $dig", line 8 - "return value of digit property of an object of $a". For $b the variable of $this will automatically accept the corresponding value.

Rules of use of $this

  • As $this is the link to the caused object given to the variable it is impossible to assign any values.

Author: «MirrorInfo» Dream Team


Print