class scope::

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FrEaKmAn
    New Member
    • Aug 2007
    • 46

    class scope::

    [php]
    class database{
    function __construct(){
    $this->connect_id = mysql_connect(' localhost','roo t','');
    if($this->connect_id){
    if (mysql_select_d b('dbname')) return $this->connect_id;
    else return $this->error();
    }else return $this->error();
    }
    function query($query){
    if ($query != NULL){
    $this->query_result = mysql_query($qu ery, $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..
  • satas
    New Member
    • Nov 2007
    • 82

    #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&funct ions. Try google for it.

    Comment

    • FrEaKmAn
      New Member
      • Aug 2007
      • 46

      #3
      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&funct ions. 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?

      Comment

      • FrEaKmAn
        New Member
        • Aug 2007
        • 46

        #4
        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::__contr uct(); and everything works :D

        only problem is how do I access parent class or the parent class? For example if I have new class somenewclassnam e extends someclassname, how do I access database class?

        Comment

        Working...