Connecting Tech Pros Worldwide Forums | Help | Site Map

Aggregate or extending a class alternatives ?

Floortje
Guest
 
Posts: n/a
#1: Jul 17 '08
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

burgermeister01@gmail.com
Guest
 
Posts: n/a
#2: Jul 17 '08

re: Aggregate or extending a class alternatives ?


On Jul 17, 7:35*am, Floortje <d...@mail.mewrote:
Quote:
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{
}
Michael Fesser
Guest
 
Posts: n/a
#3: Jul 17 '08

re: Aggregate or extending a class alternatives ?


..oO(Floortje)
Quote:
>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].
Quote:
>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
Floortje
Guest
 
Posts: n/a
#4: Jul 17 '08

re: Aggregate or extending a class alternatives ?


Michael Fesser schreef:
Quote:
.oO(Floortje)
>
Quote:
>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 :-)
Quote:
Quote:
>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
Quote:
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

Floortje
Guest
 
Posts: n/a
#5: Jul 17 '08

re: Aggregate or extending a class alternatives ?


burgermeister01@gmail.com schreef:
Quote:
Quote:
>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
Michael Fesser
Guest
 
Posts: n/a
#6: Jul 17 '08

re: Aggregate or extending a class alternatives ?


..oO(Floortje)
Quote:
>Michael Fesser schreef:
Quote:
>.oO(Floortje)
>>
Quote:
>>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.
Quote:
>Dependent on the functionality and design of those
Quote:
>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.
Quote:
>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
Floortje
Guest
 
Posts: n/a
#7: Jul 17 '08

re: Aggregate or extending a class alternatives ?


Michael Fesser schreef:
Quote:
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 !
Quote:
>
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
burgermeister01@gmail.com
Guest
 
Posts: n/a
#8: Jul 17 '08

re: Aggregate or extending a class alternatives ?


On Jul 17, 10:18*am, Floortje <d...@mail.mewrote:
Quote:
burgermeiste...@gmail.com schreef:
>
Quote:
Quote:
Floortje
>
Quote:
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:
>
Quote:
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.
Floortje
Guest
 
Posts: n/a
#9: Jul 17 '08

re: Aggregate or extending a class alternatives ?


burgermeister01@gmail.com schreef:
Quote:
>
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
Closed Thread