By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,166 Members | 1,695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,166 IT Pros & Developers. It's quick & easy.

Aggregate or extending a class alternatives ?

P: n/a
Hi i have been struggeling with this question for quite some time now.

I have some helper classes that handle images (upload an image, create
thumbnails and show a imagelist), links (add link, edit link, show
linklist), comments (add comment, show commentlist) etc.

I do not need all this functionality on every page.

Ideally I could just extend the controller like this

class Mypage extends Controlller, ImageHelper, LinkHelper{}
class AnotherPage extends Controller, CommentHelper{}

But that's not supported in php5.

Aggregate is also not supported and I dont like the beta alternatives
given on the php page.

My solution is sending the controller to the helper class so that I
have all the functions and variables in the helper classes available.
The link helper class can figure out what to do on it't own from there
and saves me lotsa time whenever I create a new set of pages.

class Mypage extends Controller(){

function _linklist(){

new LinkHelper($this);

}

public function addlink(){

new LinkHelper($this);

}

}
However this has been nagging me for quite some time now. Anyone know a
better way to do this ?

Floortje
Jul 17 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Jul 17, 7:35*am, Floortje <d...@mail.mewrote:
Hi i have been struggeling with this question for quite some time now.

I have some helper classes that handle images (upload an image, create
thumbnails and show a imagelist), links (add link, edit link, show
linklist), comments (add comment, show commentlist) etc.

I do not need all this functionality on every page.

Ideally I could just extend the controller like this

class Mypage extends Controlller, ImageHelper, LinkHelper{}
class AnotherPage extends Controller, CommentHelper{}

But that's not supported in php5.

Aggregate is also not supported and I dont like the beta alternatives
given on the php page.

My solution is sending the controller to the helper class so that I
have all the functions and variables in the helper classes available.
The link helper class can figure out what to do on it't own from there
and saves me lotsa time whenever I create a new set of pages.

class Mypage extends Controller(){

function _linklist(){

new LinkHelper($this);

}

public function addlink(){

new LinkHelper($this);

}
}

However this has been nagging me for quite some time now. Anyone know a
better way to do this ?

Floortje
This is a difficult question to answer without knowing the specifics
of your classes, but my understanding has always been that anytime you
need to add chunks of functionality, but without necessarily extending
it, an interface is the best solution to use.
So you might put some of the more general functions of your helper
classes into an interface and then do this:

class MyPage implements Controller{
}
Jul 17 '08 #2

P: n/a
..oO(Floortje)
>Hi i have been struggeling with this question for quite some time now.

I have some helper classes that handle images (upload an image, create
thumbnails and show a imagelist), links (add link, edit link, show
linklist), comments (add comment, show commentlist) etc.

I do not need all this functionality on every page.

Ideally I could just extend the controller like this

class Mypage extends Controlller, ImageHelper, LinkHelper{}
class AnotherPage extends Controller, CommentHelper{}

But that's not supported in php5.
Multiple inheritance is bad[tm].
>Aggregate is also not supported and I dont like the beta alternatives
given on the php page.
What do you mean? Dependent on the functionality and design of those
classes you could simply add instances of them to a page instance as
necessary. Composition or aggregation would work well here.

Micha
Jul 17 '08 #3

P: n/a
Michael Fesser schreef:
.oO(Floortje)
>Hi i have been struggeling with this question for quite some time now.

I have some helper classes that handle images (upload an image, create
thumbnails and show a imagelist), links (add link, edit link, show
linklist), comments (add comment, show commentlist) etc.

I do not need all this functionality on every page.

Ideally I could just extend the controller like this

class Mypage extends Controlller, ImageHelper, LinkHelper{}
class AnotherPage extends Controller, CommentHelper{}

But that's not supported in php5.

Multiple inheritance is bad[tm].
As I have read some disagree on that. Me .. I haven't made up my mind
yet :-)
>Aggregate is also not supported and I dont like the beta alternatives
given on the php page.

What do you mean?
I meand I cant use aggregate since it is not supporte in php5
Fatal error: Call to undefined function aggregate()
and this page gives a big pink warning
http://us2.php.net/manual/en/functio...kit-import.php
Dependent on the functionality and design of those
classes you could simply add instances of them to a page instance as
necessary. or aggregation would work well here.
I dont know how to add instances of them. This one level to abstract for
me. Do you mean

class A{

private $Someclass

public function __construct(){

$this->Someclass = new Someclass;

}

in wich case Somclass still doesn't extend the controllor and doesn;t
have access to it's handy functions :-)

Floortje

Jul 17 '08 #4

P: n/a
bu*************@gmail.com schreef:
>Floortje

This is a difficult question to answer without knowing the specifics
of your classes, but my understanding has always been that anytime you
need to add chunks of functionality, but without necessarily extending
it, an interface is the best solution to use.
So you might put some of the more general functions of your helper
classes into an interface and then do this:

class MyPage implements Controller{
}
Yup but that would mean i have to define every function and I dont want
that. I just want to be able to call them like

$mypage->ImageList();

And voilla ... now I have the imagelist for that page.

Or am I mistaken ?

Floortje
Jul 17 '08 #5

P: n/a
..oO(Floortje)
>Michael Fesser schreef:
>.oO(Floortje)
>>Aggregate is also not supported and I dont like the beta alternatives
given on the php page.

What do you mean?

I meand I cant use aggregate since it is not supporte in php5
Fatal error: Call to undefined function aggregate()
and this page gives a big pink warning
http://us2.php.net/manual/en/functio...kit-import.php
OK, understood. But personally I wouldn't use that.
>Dependent on the functionality and design of those
>classes you could simply add instances of them to a page instance as
necessary. or aggregation would work well here.

I dont know how to add instances of them. This one level to abstract for
me. Do you mean

class A{

private $Someclass

public function __construct(){

$this->Someclass = new Someclass;

}
Yes, something like that.
>in wich case Somclass still doesn't extend the controllor and doesn;t
have access to it's handy functions :-)
Just a matter of design. The helper doesn't directly extend the
controller, but becomes a part of it. And if you pass the instance of
the controller ($this) to the helper's constructor, it'll also know who
its parent is and will be able to call the controller's public methods.

Of course if you want to do things like

$mypage->ImageList();

it becomes a bit more difficult. You could work with the magic __call()
method to find a helper which implements an ImageList() method. But this
makes the code complicated and hard to debug.

You could also have a look at the decorator pattern, which would be a
way to extend the controller functionality by "wrapping" it into another
class, which could be wrapped again into something else if necessary.
This pattern is used for example in the SPL to implement the various
iterators.

But as said before - it's difficult to give better hints without knowing
the internals of your classes and the way they are intended to work.
Maybe the above contains some useful keywords for further reading.

Micha
Jul 17 '08 #6

P: n/a
Michael Fesser schreef:
it becomes a bit more difficult. You could work with the magic __call()
method to find a helper which implements an ImageList() method. But this
makes the code complicated and hard to debug.

You could also have a look at the decorator pattern, which would be a
way to extend the controller functionality by "wrapping" it into another
class, which could be wrapped again into something else if necessary.
This pattern is used for example in the SPL to implement the various
iterators.
Cool im looking into it right now. Seems promising !
>
But as said before - it's difficult to give better hints without knowing
the internals of your classes and the way they are intended to work.
Maybe the above contains some useful keywords for further reading.

I understand. The working of the classes is quite simple.

class Controller sets the variables the child class needs and sets the
correct template.
It contains functions like setTemplateName() and getTemplateName()
getMethod() etc.

the child class extends the controller and handles a request

a request to /mypage/index would call class $MyPage->index(). The index
function does not need to contain anything but usually it retrieves the
last couple of entries from a datbase and sends it to the template.

If I would like to make a request to a helper class I would first have
to create a dummy function.
/mypage/uploadimage/10 ->calls $mypage->upladimage();
class MyPage extends Controller{

public function uploadimage(){

new ImageHelper($this);

}

}

The image helper then looks at the Controller variables (Controller =
'MyPage' Action = 'uploadimage' and id = 10) and creates an upload form.
If an image is submitted to this function it handles the image upload
and database interactions.
I think this is quite handy since all i have to do to add image uploads
to any page is add this simple line to the controller.

However I have quite a few of these helper classes and sometimes 90% of
the functions on a page only call a helper class. Nothing really wrong
with that i guess I just had this nagging feeling that i was beeing
silly and that there was a much simple way (there usualy is):-)

Floortje
Jul 17 '08 #7

P: n/a
On Jul 17, 10:18*am, Floortje <d...@mail.mewrote:
burgermeiste...@gmail.com schreef:
Floortje
This is a difficult question to answer without knowing the specifics
of your classes, but my understanding has always been that anytime you
need to add chunks of functionality, but without necessarily extending
it, an interface is the best solution to use.
So you might put some of the more general functions of your helper
classes into an interface and then do this:
class MyPage implements Controller{
}

Yup but that would mean i have to define every function and I dont want
that. I just want to be able to call them like

$mypage->ImageList();

And voilla ... now I have the imagelist for that page.

Or am I mistaken ?

Floortje
Yea you're right. I am thinking of Java interfaces which allow the
programmer to actually implement the code within the interface. IIRC,
PHP doesn't do that.

I wish I had time to give better/more input, but unfortunetly, right
now the best I can do is suggest downloading some existing frameworks,
and see how they acheive this.
Jul 17 '08 #8

P: n/a
bu*************@gmail.com schreef:
>
Yea you're right. I am thinking of Java interfaces which allow the
programmer to actually implement the code within the interface. IIRC,
PHP doesn't do that.

I wish I had time to give better/more input, but unfortunetly, right
now the best I can do is suggest downloading some existing frameworks,
and see how they acheive this.
Np thx for the input !!

Floortje
Jul 17 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.