How to Reference an Object's Name in PHP
The PHP language is an object-oriented programming language. Objects are sets of related bits of data and functions. Since objects can consume large sections of computer memory, it is often more efficient to pass references of the object rather than the object itself. When a reference is passed to another part of the program, the program only has to look up the location of the object, rather than allocate more memory and copy the object. Referencing an object name in PHP is done with the & operator.
Instructions
-
-
1
Decide how you will run your PHP code. If you have a PHP server, you can execute code using PHP files. If you do not have access to a PHP server, you can use an online PHP interpreter. Enter the code in the tutorial into either a PHP file or the online PHP interpreter.
-
2
Begin your PHP program with the following statement:
<?php
-
-
3
Declare a new class that stores a single value. This can be done using the following code:
class ReferenceExample { public $dataType = 0;}
-
4
Create a new instance of this class using the new operator, like this:
$i = new ReferenceExample;
-
5
Create a reference to this new object using the & operator and store this reference in a variable named $r, like this:
$r = &$i;
-
6
Print the value of the object using the reference, like this:
echo $r->dataType;
-
7
Conclude your PHP program with the statement below. Your program is now ready to be tested on your PHP server or online PHP interpreter.
?>
-
1