SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Confused about class extend syntax

    I have a database class which contains the following method:

    Code:
    class Database {
    
          // Fetch array - one column
        public function fetch_array_column_ex(){
    
            $query = $this->execute();
    
            return $query->fetchAll(PDO::FETCH_COLUMN, 0);
    
        } // End fetch_array() function
    
    }
    Fine.

    I then extended the Database class like so:


    Code:
    class Node extends Database {
        
         public function fetch_array_column_user($user_id) {
            
            $STH = $this->connection->prepare("SELECT username FROM user WHERE id = :user_id" );
            
            $STH->bindParam(':user_id', $user_id, PDO::PARAM_INT);
            
            return $STH;
      
         }
         
    }// End function fetch_array_column_user()
    Instead of retuning $STH I want to call the Database method fetch_array_column_ex() but I'm getting confused about how...
    Let everyday be Christmas

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I'm not exactly sure what you want to do, but if you have a class B that extends class A you can have a class B call functions of A by using parent::

    PHP Code:
    class 
    {
       public function 
    add($a$b)
       {
            return 
    $a $b;
       }
    }

    class 
    extends A
    {
        public function 
    subtract($a$b)
        {
            return 
    parent::add($a, -1*$b);
        }

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class A  

       public function 
    add($a$b
       { 
            return 
    $a $b
       } 


    class 
    extends 

        public function 
    subtract($a$b
        { 
            return 
    $this->add($a, -1*$b); 
        } 


    $b = new B();
    echo 
    $b->subtract(21); 
    you can use the $this-> dereference operator too
    Last edited by AnthonySterling; Apr 11, 2011 at 01:04. Reason: Added missed bbcode tag. :)
    my mobile portal
    ghiris.ro

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    PHP Code:
    class A  

       public function 
    add($a$b
       { 
            return 
    $a $b
       } 


    class 
    extends 

        public function 
    subtract($a$b
        { 
            return 
    $this->add($a, -1*$b); 
        } 


    $b = new B();
    echo 
    $b->subtract(21); 
    you can use the $this-> dereference operator too
    Indeed you can, but take care that if you also define the function 'add' in class B (so class A had a function 'add' and class B has a function 'add' that overrides A::add) $this->add will call B::add, and parent::add will call A::add
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

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