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

class within a class

nathj
Expert 100+
P: 938
Hi,

I have a data abstraction class that holds all the functions for query the database.

I now have a second class that holds all the functions for writing specific data to the database.

The class definitions are all in the same php file and I need to use the first class within the second.

On the page where the functions of the second class are used I have instantiated both classes however, when I try to use the functions of the data abstraction class I get the message:
'Call to member function secure() on non object.

The question then is this. How can I use one class within another?

Cheers
nathj
Jul 25 '07 #1
Share this Question
Share on Google+
7 Replies


ilearneditonline
Expert 100+
P: 130

The question then is this. How can I use one class within another?

Cheers
nathj
Expand|Select|Wrap|Line Numbers
  1.  $yourclass = new TheClassName();
  2.  
  3. // call to function in class.
  4. $yourclass->thefunction();
  5.  
Jul 25 '07 #2

kovik
Expert 100+
P: 1,044
You'll need to send an instance of the other class to the one you want to use it in. Database objects are typically retrieved by use of a singleton, but that's not exactly a beginner's topic.

[php]class CDatabase
{
....
}

class CRetriever
{
protected $_db;
public function __construct(CDatabase $db)
{
$this->_db = $db;
}
}

$pDb = new CDatabase;
$pRetriever = new CRetriever($pDb);[/php]
Jul 26 '07 #3

nathj
Expert 100+
P: 938
[php]class CDatabase
{
....
}

class CRetriever
{
protected $_db;
public function __construct(CDatabase $db)
{
$this->_db = $db;
}
}

$pDb = new CDatabase;
$pRetriever = new CRetriever($pDb);[/php]
Hi,

That works a treat!

What do you mean by singleton? I may be relativley new to PHP but I'm an experienced software deveoper and I'm keen to learn all I can. If you could point me in the right direction on the use singleton and I'll do some research.

Cheers
nathj
Jul 26 '07 #4

kovik
Expert 100+
P: 1,044
Oh, good then.

A singleton is an object that there can only be one instance of, and it does this by storing a static version of itself (or making an outer class that you use to reference the single instance) and retrieving the instance through the singleton itself.

Okay, that feels as thought it wasn't very clear. :-p


A good explanation of a singleton is that there's only a single instance. This instance is stored in the singleton, and retrieved when you call for it. If you understand how static variables work, the single instance is a static object that all other objects retrieve.

So, instead of the code I gave you above, it'd be more like this:

Expand|Select|Wrap|Line Numbers
  1. class CDatabase
  2. {
  3.     ....
  4. }
  5.  
  6. class CDatabase_Singleton
  7. {
  8.     static protected $_pDb;
  9.  
  10.     static public function GetObject()
  11.     {
  12.         if(!self::$_pDb) self::$_pDb = new CDatabase;
  13.         return self::$_pDb;
  14.     }
  15. }
  16.  
  17. class CRetriever
  18. {
  19.     protected $_db;
  20.     public function __construct()
  21.     {
  22.         $this->_db = CDatabase_Singleton::GetObject();
  23.     }
  24. }
  25.  
  26. $pRetriever = new CRetriever();
You no longer need to send the database object to every object that needs it, AND you don't risk creating multiple connections by accident.

A C++ programmer wrote an article here on the singleton pattern, but I figured giving you a short and sweet answer before you read deeper into it would help you out more.
Jul 26 '07 #5

ilearneditonline
Expert 100+
P: 130
You'll need to send an instance of the other class to the one you want to use it in. Database objects are typically retrieved by use of a singleton, but that's not exactly a beginner's topic.

[php]class CDatabase
{
....
}

class CRetriever
{
protected $_db;
public function __construct(CDatabase $db)
{
$this->_db = $db;
}
}

$pDb = new CDatabase;
$pRetriever = new CRetriever($pDb);[/php]
Thanks, i didn't even realize you could do that with PHP. I have been using it with my c# projects, but never even thought to investigate using a singleton with PHP.
Jul 26 '07 #6

kovik
Expert 100+
P: 1,044
Thanks, i didn't even realize you could do that with PHP. I have been using it with my c# projects, but never even thought to investigate using a singleton with PHP.
Ever since PHP 5, PHP has been up there with the greats. :-D
Jul 26 '07 #7

nathj
Expert 100+
P: 938
Oh, good then.

A singleton is an object that there can only be one instance of, and it does this by storing a static version of itself (or making an outer class that you use to reference the single instance) and retrieving the instance through the singleton itself.

Okay, that feels as thought it wasn't very clear. :-p


A good explanation of a singleton is that there's only a single instance. This instance is stored in the singleton, and retrieved when you call for it. If you understand how static variables work, the single instance is a static object that all other objects retrieve.

So, instead of the code I gave you above, it'd be more like this:

Expand|Select|Wrap|Line Numbers
  1. class CDatabase
  2. {
  3.     ....
  4. }
  5.  
  6. class CDatabase_Singleton
  7. {
  8.     static protected $_pDb;
  9.  
  10.     static public function GetObject()
  11.     {
  12.         if(!self::$_pDb) self::$_pDb = new CDatabase;
  13.         return self::$_pDb;
  14.     }
  15. }
  16.  
  17. class CRetriever
  18. {
  19.     protected $_db;
  20.     public function __construct()
  21.     {
  22.         $this->_db = CDatabase_Singleton::GetObject();
  23.     }
  24. }
  25.  
  26. $pRetriever = new CRetriever();
You no longer need to send the database object to every object that needs it, AND you don't risk creating multiple connections by accident.

A C++ programmer wrote an article here on the singleton pattern, but I figured giving you a short and sweet answer before you read deeper into it would help you out more.
Thanks for the explanation. It seems like a good way forward.
I think I'll make some adjustments to my code. At present my db object is created and destryoed on every page.

The result is that there are never two instances but there is a lot of wasted processing.

Thanks for the tip off.

Cheers
nathj
Jul 27 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.