Reply to Re: OO PHP Referencing an instance of a class

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация