use one class within another?
Posted: Sat Dec 27, 2008 4:58 pm
Hi
I was wondering if it's possible to create a class instance and use it like a normal variable within another class.
Example:
Hopefully that makes sense?
I can't seem to figure out how to do this and if i try the above I get an error complaining about the usage of "new" in the second class.
I'm trying to make one class where each instance would be a column for a database with different settings (required, input validation regex etc) and then the second class will be more of a "row" where each column or member variable will be an instance of the first class with its own specific settings. I have a whole lot of data to manage and each database column requires special validation and other details and the main point here is I'm trying to make a solution where I can easily add a column if necessary (just add another instance of class1 and set it up).
Is there a better way to do this instead?
Thanks!
I was wondering if it's possible to create a class instance and use it like a normal variable within another class.
Example:
Code: Select all
class test1
{
var $message = "test message"
function writeMessage()
{
echo $this->message;
}
// obviously the real class is alot more useful ;)
}
class test2
{
var $test1_class = new test1();
// some more useful vars
// and some useful functions too
}
$myclass = new test2();
$myclass->test1_class->writeMessage();
I can't seem to figure out how to do this and if i try the above I get an error complaining about the usage of "new" in the second class.
I'm trying to make one class where each instance would be a column for a database with different settings (required, input validation regex etc) and then the second class will be more of a "row" where each column or member variable will be an instance of the first class with its own specific settings. I have a whole lot of data to manage and each database column requires special validation and other details and the main point here is I'm trying to make a solution where I can easily add a column if necessary (just add another instance of class1 and set it up).
Is there a better way to do this instead?
Thanks!