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

How to dynamically generate functions in a class

P: n/a
I am having to be tasked with a rather painful task of updating an
existing class by adding a dynamic amount of database field values;
for each database field value there has to be a generated array inside
the class plus a function "get method" to return it.

I do not want to have to constantly type out:

function getStuff() {
return $this->stuff;
}

for every occurrence of stuff I find that is generated by the database
SELECT call on the fields of as many as 20 different tables!! This is
enormous and I would love a more dynamic, functional method to
generate.. methods.

Can someone give me an idea of how this can be done?

Thanx
Phil
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 14 Oct 2003 14:29:49 -0700, so*****@erols.com (Phil Powell) wrote:
I am having to be tasked with a rather painful task of updating an
existing class by adding a dynamic amount of database field values;
for each database field value there has to be a generated array inside
the class plus a function "get method" to return it.

I do not want to have to constantly type out:

function getStuff() {
return $this->stuff;
}

for every occurrence of stuff I find that is generated by the database
SELECT call on the fields of as many as 20 different tables!! This is
enormous and I would love a more dynamic, functional method to
generate.. methods.

Can someone give me an idea of how this can be done?


Not that it helps now, but PHP5 has improvments to the object features to
allow autoloaded methods, similar to 'sub AUTOLOAD' in Perl classes.

http://www.php.net/zend-engine-2.php

(scroll to the bottom).

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2

P: n/a
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
I am having to be tasked with a rather painful task of updating an
existing class by adding a dynamic amount of database field values;
for each database field value there has to be a generated array inside
the class plus a function "get method" to return it.

I do not want to have to constantly type out:

function getStuff() {
return $this->stuff;
}

for every occurrence of stuff I find that is generated by the database
SELECT call on the fields of as many as 20 different tables!! This is
enormous and I would love a more dynamic, functional method to
generate.. methods.

Can someone give me an idea of how this can be done?


I'm not quite sure I understand the problem correctly but this may solve it.
Why not basically store all the results of your select statements in one
array and use the first key of this array as your tablename, i.e.
$stuff['info'] contains the resultset from table 'info' and is an array
("Field1" => "Value1", "Field2" => "Value2", etc) and then you can just
refer to this array of arrays within your methods.

It's never a good idea to repeat code or functionality and is a sure sign of
a better approach to the design of the algorithm.

Paulus
Jul 17 '05 #3

P: n/a
Phil Powell wrote on Tuesday 14 October 2003 14:29:
I am having to be tasked with a rather painful task of updating an
existing class by adding a dynamic amount of database field values;
for each database field value there has to be a generated array inside
the class plus a function "get method" to return it.

I do not want to have to constantly type out:

function getStuff() {
return $this->stuff;
}


This is not an answer to your question; but out of pure curiosity - why are
you being asked to do it that way? In other words, instead of

function getStuff()

why not:

function get($stuff)

Just curious. I think Paulus suggested something similar too.

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.