I want to define a set of classes then based on a users input create data from those classes.
How can I do this?
For instance lets say I have defined a bunch of classes as data types that include functions that do validation, sanitation, display etc for that data class.
In a form I plan to ask the user to select a data type and use this to create a field in a multipart record. This data type might be used over and over again as the definition for multiple fields. I will be eventually saving this data to a MySQL database as well.
How do I go about using these classes now to create an actual data set?
And then how can I use that data set to call the functions that deal with that data set?
Also to add to the complexity of this the data sets will be defined separately as "plugins" to support new data sets as new ones are developed.
Defining a class then dynamically calling it ?
Started by redheadedrod, Oct 18 2010 07:02 AM
9 replies to this topic
#1
Posted 18 October 2010 - 07:02 AM
|
|
|
#2
Posted 18 October 2010 - 07:14 AM
If I understand correctly, you want to build a type of framework? Files generated from the user input containing all the "default" functions and variables?
#3
Posted 18 October 2010 - 07:22 AM
I suggest you define different kind of fields, how they are validated and stuff, then, based on this fields, you store your data vertically in a field data table (i.e. one field per row) where you connect all fields to one post, then you can keep all data together and still have a dynamic structure on your data saving, as new fields only need new rows, not new columns, and can be combined in any way...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#4
Posted 18 October 2010 - 07:39 AM
I am relatively new to PHP and OOP in general. I understand MOST of the concepts but trying to figure out how to actually make them work.
I have a very BASIC understanding of such things as frameworks and such.
I guess if you can define it as a dynamic framework then the "plugin" as I call them would be somewhat.
I am trying to make a modular program that will use data structures that are defined and developed separately from the main program.
The program then will use these to build the content.
The program with user input will use the classes and go from there.
I am trying to figure out how to dynamically set the variables...
Then how to call the functions in those variables based on the class...
So if I have a defined class called birthdate...
The user in a form decides to use this class and calls it "User_birthdate".
If I were hard coding it I would just use:
$User_birthdate = new $birthdate();
However in this case I can't do that.. How would I set this up?
And then if I had a function in the class birthdate that was age() and I would normally hard code it as...
$User_birthdate->age();
But this obviously won't work in this instance. So how would I use them?
Obviously for security and compatibility reasons I would use a prefix for this so the actual variable might start out as $MyProgram_User_birthdate.
I have a very BASIC understanding of such things as frameworks and such.
I guess if you can define it as a dynamic framework then the "plugin" as I call them would be somewhat.
I am trying to make a modular program that will use data structures that are defined and developed separately from the main program.
The program then will use these to build the content.
The program with user input will use the classes and go from there.
I am trying to figure out how to dynamically set the variables...
Then how to call the functions in those variables based on the class...
So if I have a defined class called birthdate...
The user in a form decides to use this class and calls it "User_birthdate".
If I were hard coding it I would just use:
$User_birthdate = new $birthdate();
However in this case I can't do that.. How would I set this up?
And then if I had a function in the class birthdate that was age() and I would normally hard code it as...
$User_birthdate->age();
But this obviously won't work in this instance. So how would I use them?
Obviously for security and compatibility reasons I would use a prefix for this so the actual variable might start out as $MyProgram_User_birthdate.
#5
Posted 18 October 2010 - 08:00 AM
Maybe I should be more descriptive of what I am building and trying to attempt...
The concepts I learn building this will carry me into other projects as well...
My first project I will try using this in is a Module for the CMS Xoops. I want to build a highly flexible profile module that allows new data types to be added as separate add ons as opposed to totally redoing the code.
what I am discussing here is how to add support for new and different data types the original module had no clue about.
At the moment it looks like defining the majority of them as classes will work. Maybe I am totally on the wrong track here.
The intention is that the admin will use these new data types to build the profile design. So there will be two different data sets saved in the data base.
One would define the design of the profile including all the fields to be included in a users profile based on the above mentioned data types. This would include the information required to actually setup the particular data type. Such as name for the data type, validation information (Such as age range for the above mentioned example) and any other related information.
The other data set would contain the actual user data based on the design as defined in the other data set.
Since this stuff is dynamically defined I am unsure how to then access things and actually make it work.
The concepts I learn building this will carry me into other projects as well...
My first project I will try using this in is a Module for the CMS Xoops. I want to build a highly flexible profile module that allows new data types to be added as separate add ons as opposed to totally redoing the code.
what I am discussing here is how to add support for new and different data types the original module had no clue about.
At the moment it looks like defining the majority of them as classes will work. Maybe I am totally on the wrong track here.
The intention is that the admin will use these new data types to build the profile design. So there will be two different data sets saved in the data base.
One would define the design of the profile including all the fields to be included in a users profile based on the above mentioned data types. This would include the information required to actually setup the particular data type. Such as name for the data type, validation information (Such as age range for the above mentioned example) and any other related information.
The other data set would contain the actual user data based on the design as defined in the other data set.
Since this stuff is dynamically defined I am unsure how to then access things and actually make it work.
#6
Posted 18 October 2010 - 08:44 AM
you can create dynamical objects and call dynamical methods in these...
$obj = new $myClassName; // creates an object of the class named as value of variable $myClassName
$obj->{$method}(); // calls the method in $obj with the name as value in $method
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#7
Posted 24 October 2010 - 04:56 AM
Ok, Lets say I have stored the class name and a name to use as a variable in a data base and I wish to pull them out and use them to actually create my object and call it how would I do that?
Lets say they have been read from the database and are put into variables $temp[1](classtype) and $temp[0] (name)
But I KNOW it has a method named output();
could I use something like this?
$obj.{temp[0]} = new $temp[1];
And then call the function like this?
$obj.{temp[0]}->output();
Or is there a simpler way to do this?
I basically want to build my variables from scratch from information stored in the data base.
I suppose there is another way to do this if I use polymorphing (Assuming this is similar to C++) but I haven't touched onto the more advanced aspects of OOP yet.
Thanks!
Rodney
Lets say they have been read from the database and are put into variables $temp[1](classtype) and $temp[0] (name)
But I KNOW it has a method named output();
could I use something like this?
$obj.{temp[0]} = new $temp[1];
And then call the function like this?
$obj.{temp[0]}->output();
Or is there a simpler way to do this?
I basically want to build my variables from scratch from information stored in the data base.
I suppose there is another way to do this if I use polymorphing (Assuming this is similar to C++) but I haven't touched onto the more advanced aspects of OOP yet.
Thanks!
Rodney
#8
Posted 24 October 2010 - 05:53 AM
Why are you templating a progamming language, rather than using a programming language to template, for the profile? This is a crazy thread.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#9
Posted 27 November 2010 - 08:16 PM
NullWorm, if you noticed in the above messages I said I am new and such.. Looking for ways to do this...
Your questioning my method does nothing to help me or expand my knowledge.
If you wanted to be helpful it would have been more productive to tell me that I am going all about it wrong and offer a new way to look into. I don't expect anyone to spoon feed me but would have at least been nice to been pointed in the right direction instead of basically being called stupid, or more accurately "crazy".
Orjan, thank you for your helpful post.
As it turns out I had a "light bulb" moment and have figured out a whole other path to follow to accomplish this goal.
I can store the information I need in the database but I need a way to call a function that will be built on the fly.
The idea here is I will have a series of "modules" and each will have set of the same functions. To make it simple lets call them input() and output(). They will be prefixed with the name of the module so If I have two modules named one and two... I would include the modules into my code then call the functions... The module name will be stored in a database.
So I have two module files that need to be included...
module one has two functions
one_output()
one_input()
module two has two functions
two_output()
two_input()
To call these functions could I then build the function name into a variable then call it in the following fashion assuming $modulename holds the name of the module I want to call then I build the function name in a variable named $fname and then call it like this?
$fname = $modulename . "_output";
{$fname}("Data to output");
If this wont work is there another way to do this?
Thanks!
Your questioning my method does nothing to help me or expand my knowledge.
If you wanted to be helpful it would have been more productive to tell me that I am going all about it wrong and offer a new way to look into. I don't expect anyone to spoon feed me but would have at least been nice to been pointed in the right direction instead of basically being called stupid, or more accurately "crazy".
Orjan, thank you for your helpful post.
As it turns out I had a "light bulb" moment and have figured out a whole other path to follow to accomplish this goal.
I can store the information I need in the database but I need a way to call a function that will be built on the fly.
The idea here is I will have a series of "modules" and each will have set of the same functions. To make it simple lets call them input() and output(). They will be prefixed with the name of the module so If I have two modules named one and two... I would include the modules into my code then call the functions... The module name will be stored in a database.
So I have two module files that need to be included...
module one has two functions
one_output()
one_input()
module two has two functions
two_output()
two_input()
To call these functions could I then build the function name into a variable then call it in the following fashion assuming $modulename holds the name of the module I want to call then I build the function name in a variable named $fname and then call it like this?
$fname = $modulename . "_output";
{$fname}("Data to output");
If this wont work is there another way to do this?
Thanks!
#10
Posted 28 November 2010 - 10:13 AM
I would call them input() and output() in both classes, and then create an object, and on that object call the function, this way, the name is equal, and you therefore don't have to think about if it is of type one or type two. the very best would be if you specify an interface you connect all your classes to, so they are demanded to have the input and output functions.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall


Sign In
Create Account

Back to top









