473,320 Members | 1,900 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,320 software developers and data experts.

class scope::

[php]
class database{
function __construct(){
$this->connect_id = mysql_connect('localhost','root','');
if($this->connect_id){
if (mysql_select_db('dbname')) return $this->connect_id;
else return $this->error();
}else return $this->error();
}
function query($query){
if ($query != NULL){
$this->query_result = mysql_query($query, $this->connect_id);
if(!$this->query_result){
return false;
}else{
return $this->query_result;
}
}else{
return false;
}
}
}
[/php]I want to access query from other class like this:

[php]
database::query("UPDATE users SET ....");
[/php]but I can't access it, any ideas? I think I should update database class because I read somewhere that I can't access $this-> from other class..

edit: I know about this solution
[php]
$db = new database();
$db->query...
[/php]and it works, but I don't know, I don't like when I need to "recreate" class..
Mar 18 '08 #1
3 1254
satas
82
Have a look to singleton pattern in PHP.
It is exactly what you need. The main idea it that you don't need to create an instance of dbConnection class every time you use it. It can be done via static variables&functions. Try google for it.
Mar 19 '08 #2
Have a look to singleton pattern in PHP.
It is exactly what you need. The main idea it that you don't need to create an instance of dbConnection class every time you use it. It can be done via static variables&functions. Try google for it.
hm, my next solution is that I use

[PHP]
$GLOBALS['db']->query(....
[/PHP]

as $db is already created. Would this be optimal solution?
Mar 19 '08 #3
hello again

what I did is I created new class someclassname, which extends database class. Then we I construct it, I also construct database class suing parent::__contruct(); and everything works :D

only problem is how do I access parent class or the parent class? For example if I have new class somenewclassname extends someclassname, how do I access database class?
Mar 20 '08 #4

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

Similar topics

2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
9
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. ...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
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...
5
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late...
0
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.