use one class within another?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ttlgreen
Forum Newbie
Posts: 2
Joined: Sat Dec 27, 2008 4:45 pm

use one class within another?

Post 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!
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: use one class within another?

Post 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
 }
 
ttlgreen
Forum Newbie
Posts: 2
Joined: Sat Dec 27, 2008 4:45 pm

Re: use one class within another?

Post 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..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: use one class within another?

Post 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
 
Post Reply