A BIT DILUTED... but it's alright!
In the PHP example above, the function foo($obj), will actually create a $foo property to "any object" passed to it - which brings some confusion to me:
$obj = new stdClass();
foo($obj); // tags on a $foo property to the object
// why is this method here?
Furthermore, in OOP, it is not a good idea for "global functions" to operate on an object's properties... and it is not a good idea for your class objects to let them. To illustrate the point, the example should be:
<?php
class A {
protected $foo = 1;
public function getFoo() {
return $this->foo;
}
public function setFoo($val) {
if($val > 0 && $val < 10) {
$this->foo = $val;
}
}
public function __toString() {
return "A [foo=$this->foo]";
}
}
$a = new A();
$b = $a; $b->setFoo(2);
echo $a->getFoo() . '<br>';
$c = new A();
$d = &$c; $d->setFoo(2);
echo $c . '<br>';
$e = new A();
$e->setFoo(16); echo $e;
?>
- - -
2
A [foo=2]
A [foo=1]
- - -
Because the global function foo() has been deleted, class A is more defined, robust and will handle all foo operations... and only for objects of type A. I can now take it for granted and see clearly that your are talking about "A" objects and their references. But it still reminds me too much of cloning and object comparisons, which to me borders on machine-like programming and not object-oriented programming, which is a totally different way to think.