The drop is always movingYou know that saying about standing on the shoulders of giants? Drupal is standing on a huge pile of midgetsAll content management systems suck, Drupal just happens to suck less.Popular open source software is more secure than unpopular open source software, because insecure software becomes unpopular fast. [That doesn't happen for proprietary software.]Drupal makes sandwiches happen.There is a module for that

PHP5 objects and references

Submitted by nk on Sun, 2009-10-25 12:35

My favorite analogue for references is a huge filing cabinet where each drawer is a variable. The label on the drawer is the name of the variable, the contents of the drawer is the value of the variable. A reference is just another label on the drawer. However, in reality, there are special variables which PHP call resources. For example, for a MySQL database connection, you will only find a little note in the drawer saying "I am not a normal variable, find the special cabinet called MySQL connections, and drawer 1 there has the connection data". In PHP5, the objects behave exactly like these resources: the little note says "I am object, find the special cabinet called "objects of class foo" and drawer X contains my data). When you pass a variable to a function, you copy the contents of the drawer -- but for objects or resources, the copy of the note will say the exact same. Example:

<?php
$foo
= new whatever(); // First object this program creates.
bar($foo);
function
bar($object) {
 
$object->x = 'y';
}
?>

So what bar receives is not the contents of the $foo object, instead a note "we are dealing with object 1".

Note that this mechanism is not the same as a reference:

<?php
class test {
  private
$test;
  function
__construct($data) {
   
$this->test = $data;
  }
  function
__toString() {
    return
$this->test;
  }
}

function
test1($object) {
 
$object = 4;
}

function
test2(&$object) {
 
$object = 4;
}

$x = new test('test');
test1($x);
print
"$x\n";
test2($x);
print
"$x\n";
?>

Gives you test and 4 as an output. Why? Because the first function gets a new drawer with the note saying "hey, your input is this nice object 1" and when $object = 4 runs, then PHP throws out that note and replaces it with the number 4. When the second function runs, then it operates on the same drawer, object being a mere new label the drawer beside the original label x so when $object = 4; runs, this single one drawer gets replaced, the original object lost. If you were only operating on the properties and methods of the function then the difference would not be there.

Let me add one more fun piece with references: when you are passing around an array containing a reference:

<?php
function test1($array) {
 
$array['foo'] = 1;
 
$array[] = 1;
}
function
test2(&$array) {
 
$array['foo'] = 2;
 
$array[] = 1;
}
$bar = 0;
$a = array('foo' => &$bar);
test1($a);
print
"$bar\n";
print
'$a[0]: '. $a[0] . "\n";
test2($a);
print
"$bar\n";
print
'$a[0]: '. $a[0] . "\n";
?>

The output is

1
$a[0]:
2
$a[0]: 1

Commenting on this Story is closed.