|
Posted by "Richard Lynch" on 01/20/06 00:33
On Tue, January 17, 2006 8:56 am, Henrik Gemal wrote:
> In a image gallery I have to class'es:
>
> class GPicFilePicture extends GPicFileType
> class GPicFileMovie extends GPicFileType
>
> both of them are based on:
>
> abstract class GPicFileType
>
> In my code I need to create a new GPicFilePicture. To avoid duplicated
> code I've create a wrapper class:
>
> class GPicFile
>
> that does something like this:
>
> if (fileextension == "jpg")
> return new GPicFilePicture();
> else
> return new GPicFileMovie();
This isn't in the constructor, is it?
You would need to do something like:
class GPicFile {
var $implementor = NULL;
function implementor($filename){
if (fileextension == 'jpg')
$this->implementor = new GPicFilePicture();
else
$this->implementor = new GPicFileMovie();
}
function getFileDate(){
return $this->implementor->getFileDate();
}
}
You can't have a wrapper class that returns one class or another.
You could have a FUNCTION that would return one or the other, and then
you'd have to make sure you handled either kind of object, but not a
class that pretends to be two different kinds of objects.
> so my PHP code looks like:
> $file = new GPicFile($filename);
> getFileDate() is implemented in both GPicFilePicture and
> GPicFileMovie.
>
> Now I try to do:
> $file->getFileDate();
>
> I get an error saying:
> Call to undefined method GPicFile::getFileDate()
You can't make really make GPicFile object sometimes be a
GPicFilePicture and sometimes be a GPicFileMovie...
You could sort of do this if PHP had multiple inheritence, but it
doesn't.
There might be something in the fancy new PHP5 stuff about interface
and whatnot that would be more elegant than the hack above.
But you're on your own for that.
--
Like Music?
http://l-i-e.com/artists.htm
[Back to original message]
|