note 86042 added to language.oop5.references
| From: | wbcarts at juno dot com | Date: | Tue, 30 Sep 2008 20:23:46 +0000 |
| Subject: | note 86042 added to language.oop5.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144912@lists.php.net to get a copy of this message | ||
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; // $a and $b are copies of the same identifier
// ($a) = ($b) = <id>
$b->setFoo(2);
echo $a->getFoo() . '<br>';
$c = new A();
$d = &$c; // $c and $d are references
// ($c,$d) = <id>
$d->setFoo(2);
echo $c . '<br>';
$e = new A();
$e->setFoo(16); // will be ignored
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.
----
Server IP: 69.147.83.197
Probable Submitter: 67.150.5.87
----
Manual Page -- http://www.php.net/manual/en/language.oop5.references.php
Edit -- https://master.php.net/note/edit/86042
Del: integrated -- https://master.php.net/note/delete/86042/integrated
Del: useless -- https://master.php.net/note/delete/86042/useless
Del: bad code -- https://master.php.net/note/delete/86042/bad+code
Del: spam -- https://master.php.net/note/delete/86042/spam
Del: non-english -- https://master.php.net/note/delete/86042/non-english
Del: in docs -- https://master.php.net/note/delete/86042/in+docs
Del: other reasons-- https://master.php.net/note/delete/86042
Reject -- https://master.php.net/note/reject/86042
Search -- https://master.php.net/manage/user-notes.php