www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP - using extend in classes

  1. #1
    Join Date
    Aug 2008
    Posts
    42

    PHP - using extend in classes

    Hi Guys,

    I've always included a small php file with the connection and then written classes that make a query and handle the return data.

    This time, I am trying a different approach.

    I created a class for the database connection and am using EXTEND for subsequent classes.

    For example, I have:
    Code:
    class dao{
      function connect_db(){
        //connector function
      }
    
      function query($sql){
         $this->query = mysql_query($sql);
         return $this->query;
       }
    
    }
    
    class website extends dao{
        function getUsers(){
         //get user function
       }
    }
    My question is how do I run the dao->query function from within the getUsers function?

    Would it be something like:
    Code:
    function getUsers(){
     $result = parent::query("select * from tbl_user");
     while($r = mysql_fetch_assoc($result)){
      echo $r["user_name"].'<br />';
    }
    }

    or can I just call it as if it were in the same class:

    Code:
    function getUsers(){
     $result =$this->query("select * from tbl_user");
     while($r = mysql_fetch_assoc($result)){
      echo $r["user_name"].'<br />';
    }
    }

    Still trying to get a hang of classes and such. Thanks for you patience and help.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    Quote Originally Posted by saturnprods View Post
    Hi Guys,
    ...or can I just call it as if it were in the same class...
    Yes (as long as you did not declare the method to be "private" in the parent class).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    PS: Note that the MySQLi and PDO database extensions are already object-oriented, so you could simply(?) use them instead of creating your own OO implementation of the old MySQL extension.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Aug 2008
    Posts
    42
    Quote Originally Posted by nogdog View Post
    ps: Note that the mysqli and pdo database extensions are already object-oriented, so you could simply(?) use them instead of creating your own oo implementation of the old mysql extension.
    you mean i've been reinventing fire and the wheel???

    :d

    thanks a million. I don't know how i missed this....[turns red]

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    Quote Originally Posted by saturnprods View Post
    you mean i've been reinventing fire and the wheel???

    :d

    thanks a million. I don't know how i missed this....[turns red]
    Probably because the MySQLi extension came out with PHP5, but there is an awful lot of legacy PHP4- code out there using the older MySQL extension, and even new code that feels a need to be PHP4-compatible (even though it's been close to a year now since PHP4 has ceased being supported in any way).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Aug 2008
    Posts
    42
    NogDog, this has worked great. Thanks for your help.

    But is it possible to write classes that extend this mysqli class?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    Quote Originally Posted by saturnprods View Post
    NogDog, this has worked great. Thanks for your help.

    But is it possible to write classes that extend this mysqli class?
    Yes, for instance:
    PHP Code:
    class Foo extends mysqli
    {
       public function 
    __construct($host$user$password$db)
       {
          
    parent::__construct($host$user$password$db);
       }

       public function 
    bar()
       {
          echo 
    "This is my new method added to the mysqli class";
       }

    But it all depends on what you actually need to do: perhaps it makes more sense to pass a MySQLi object to another class, for instance.
    PHP Code:
    class Foo
    {
       private 
    $db;

       public function 
    __construct(MySQLi $db)
       {
          
    $this->db $db;
       }

       public function 
    bar($id)
       {
          
    $stmt $this->db->prepare("SELECT * FROM `table` WHERE `id` = ?");
          
    $stmt->bind_param('i'$id);
          
    // and so forth...
       
    }
    }

    $db = new MySQLi('localhost''user''password''database');
    $foo = new Foo($db);
    $foo->bar(); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles