473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Aggregate or extending a class alternatives ?

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
8 2321
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
..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
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
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
..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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
3
by: Christian Christmann | last post by:
Hi, I'm working on my first STL program. My class BitSet has three private attributes size, curbit and the STL bit_vector data which contains some bits. Here is the code for the constructors: ...
2
by: Kiel | last post by:
===== My error is: error C2512: no appropriate default constructor available I'm trying to use the default args for class B when it is an aggregate of class A. I could solve this problem with...
4
by: Matt | last post by:
Hi, I've been thinking about how to do this, but can't think of a solution. I have a class that is derived from System.Web.UI.WebControls.DataGrid which works a treat, but I'd like to extend...
1
by: Pascal Polleunus | last post by:
Hi, Is there an *easy* way to display a list of tables with their number of input values in psql ? Wouldn't it be useful to have the ability to execute aggregate functions with the \d command...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
1
by: R.A.M. | last post by:
Hello, I am learning SQL Server 2005. I have (correctly) written in .NET assembly DemoSQLServer with aggregate function AvgNoMinMax in class Demo and I have added assembly to database...
14
by: Frederick Gotham | last post by:
How do we initialise an aggregate member object of a class? The following won't compile for me: struct MyStruct { int i; double d; }; class MyClass { private:
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.