Connecting Tech Pros Worldwide Forums | Help | Site Map

class scope::

Member
 
Join Date: Aug 2007
Posts: 46
#1: Mar 19 '08
[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..

Member
 
Join Date: Nov 2007
Location: Russia, Saint-Petersburg
Posts: 82
#2: Mar 19 '08

re: class scope::


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.
Member
 
Join Date: Aug 2007
Posts: 46
#3: Mar 19 '08

re: class scope::


Quote:

Originally Posted by satas

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?
Member
 
Join Date: Aug 2007
Posts: 46
#4: Mar 20 '08

re: class scope::


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