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
7 2130
The question then is this. How can I use one class within another?
Cheers
nathj
- $yourclass = new TheClassName();
-
-
// call to function in class.
-
$yourclass->thefunction();
-
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]
[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
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: - class CDatabase
-
{
-
....
-
}
-
-
class CDatabase_Singleton
-
{
-
static protected $_pDb;
-
-
static public function GetObject()
-
{
-
if(!self::$_pDb) self::$_pDb = new CDatabase;
-
return self::$_pDb;
-
}
-
}
-
-
class CRetriever
-
{
-
protected $_db;
-
public function __construct()
-
{
-
$this->_db = CDatabase_Singleton::GetObject();
-
}
-
}
-
-
$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.
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.
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
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: - class CDatabase
-
{
-
....
-
}
-
-
class CDatabase_Singleton
-
{
-
static protected $_pDb;
-
-
static public function GetObject()
-
{
-
if(!self::$_pDb) self::$_pDb = new CDatabase;
-
return self::$_pDb;
-
}
-
}
-
-
class CRetriever
-
{
-
protected $_db;
-
public function __construct()
-
{
-
$this->_db = CDatabase_Singleton::GetObject();
-
}
-
}
-
-
$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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Neil Zanella |
last post by:
Hello,
Suppose I have some method:
Foo::foo() {
static int x;
int y;
/* ... */
}
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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. ...
|
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,...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |