473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

class within a class

nathj
938 Expert 512MB
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
7 2130
ilearneditonline
130 Expert 100+

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
1,044 Expert 1GB
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
938 Expert 512MB
[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
1,044 Expert 1GB
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
130 Expert 100+
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
1,044 Expert 1GB
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
938 Expert 512MB
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

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

Similar topics

30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
19
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works"...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
7
by: andy | last post by:
A question about about passing a class by reference: Say you have a class called car, and within that you have two objects called car01 and car02. Within the class I have an int variable...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
5
by: hurricane_number_one | last post by:
I'm trying to have a class, which uses threads be able to raise events to the form that created it. I've seen solutions around the net for this, but I didn't really like their implementation. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.