Re: objects handling objects
| From: | Steve Cayford | Date: | Thu, 04 Apr 2002 23:25:00 +0000 |
| Subject: | Re: objects handling objects | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-91632@lists.php.net to get a copy of this message | ||
On Thursday, April 4, 2002, at 04:21 PM, Erik Price wrote:
I looked in the manual, but didn't see anything about this. I've read that PHP isn't a true object-oriented language, but rather simulates elements of object-oriented programming. Can I write a class that performs operations and manipulates objects? Can objects be placed into arrays etc? Erik The real hindrance I've come up against is that you can't do chained method calls, otherwise objects perform pretty well as expected. Keep in mind the difference between passing by reference and by copy, though, or you'll find yourself updating a copy of an object somewhere instead of the original.-Steve Here's some code I was just playing around with: <?php class classA {
function method1() {
return 1;
}
function method2() {
return 2;
}
}
class classB {
function method1($num = 0) {
return $num + 10;
}
function method2($num = 0) {
return $num+20;
}
}
class classC {
function echoMethod($object) {
return $object;
}
}
class classD {
function otherMethod1($object) {
return $object->method1();
}
function otherMethod2($object) {
return $object->method2();
}
}
$a = new classA;
$b = new classB;
$c = new classC;
$d = new classD;
print("a1: " . $a->method1() . "<br />\n");
print("a2: " . $a->method2() . "<br />\n");
print("b1: " . $b->method1($a->method1()) . "<br />\n");
print("b2: " . $b->method2($a->method2()) . "<br />\n");
print("d1: " . $d->otherMethod1($b) . "<br />\n");
print("d2: " . $d->otherMethod2($c->echoMethod($a)) . "<br />\n");
// this next line would generate a parse error
// print("c:a1: " . $c->echo($a)->method1() . "<br />\n");
// this outputs:
// a1: 1
// a2: 2
// b1: 11
// b2: 22
// d1: 10
// d2: 2
?>
---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php