|
Posted by Justin Koivisto on 04/11/06 20:10
Areric wrote:
> Ok all im havign some issues with a class im writing was hoping you all
> could help. I dont have access to the actual code here but ill write
> out an example of what im trying to do and maybe you all could provide
> some pointers.
<snip>
> So any ideas?
PHP5 is so nice for this kind of thing...
<?php
class Foo {
private $subObj = NULL;
public function __construct(){
// create an instance of the class we want to use in here
$this->subObj = new Bar();
}
public function __call($method,$args){
// this method is called if we attempt to call a method that is
// not declared in the class definition
if(method_exists($this->subObj,$method)){
return $this->subObj->$method($args);
}else{
echo 'Method does not exist: ',$method;
return FALSE;
}
}
public function fooWorld($string){
echo "<p>Foo: $string</p>\n";
return TRUE;
}
}
class Bar {
public function BarWorld($string){
// since __call will send the method parameters as an array as
// above, we want to compensate for that case as well. May be better
// design to expect an array of parameters to begin with(?)
if(is_array($string)){
$string=join($string);
}
echo "<p>Bar: $string</p>\n";
return TRUE;
}
}
$foo = new Foo();
$foo->fooWorld('Foo string');
$foo->barWorld('Bar string');
/*
OUTPUT:
<p>Foo: Foo string</p>
<p>Bar: Bar string</p>
*/
?>
See:
http://www.php.net/manual/en/language.oop5.overloading.php
HTH
--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Navigation:
[Reply to this message]
|