Page 1 of 1

use one class within another?

Posted: Sat Dec 27, 2008 4:58 pm
by ttlgreen
Hi
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();
 
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!

Re: use one class within another?

Posted: Sat Dec 27, 2008 5:30 pm
by cptnwinky
Your going to have to extend the first class. Then you can use the scope resolution operator ( :: ) to access the parent classes members and methods.

Code: Select all

 
 class test1
 {
     var $message = "test message"
  
     function writeMessage()
     {
          echo $this->message;
     }
     // obviously the real class is alot more useful <!-- s;) --><img src=\"{SMILIES_PATH}/icon_wink.gif\" alt=\";)\" title=\"Wink\" /><!-- s;) -->
 }
  
 class test2 extends test1
 {
      var $test1_class;
      $test1_class = class1::writeMessage();
      // some more useful vars
  
     // and some useful functions too
 }
 

Re: use one class within another?

Posted: Sat Dec 27, 2008 9:44 pm
by ttlgreen
Hi,
Thanks for the response.

I've realized over the past couple hours that even though my question was related to syntax basically, my problem is moreso one of programming strategy. I just need to completely re-think how I deal with all this database data. I'm still learning so the way I might tackle a giant database might be completely rediculas compared to how someone more experienced would. My site is basically a giant interactive spreadsheet so the entire thing is dependant on how well I interface with the database.

Anyway I'm going back to the design phase for now ;)

Thanks

EDIT: PS: I love your signature! I'm so stealing that quote..

Re: use one class within another?

Posted: Sun Dec 28, 2008 1:24 am
by josh

Code: Select all

 
class A { var $var; }
class B { function var() { return 'this is b'; } }
class C { function var() { return 'this is c'; } }
$a = new A();
$a->var = new B();
echo  $a->var(); // this is b
$a->var = new C();
echo  $a->var(); // this is c