www.webdeveloper.com
Results 1 to 2 of 2

Thread: rederencing other sub classes inside class

  1. #1
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062

    Cool rederencing other sub classes inside class

    Hi,

    I am making a simple PHP framework, this has a 'base' class, then includes other classes, like a database abstraction class and a form validation class.

    I have got the classes all working and including, but I cant seam to reference another sub class from with in a sub class?

    example:
    PHP Code:
    <?php

        
    require_once('db.php'); // databse class
        
    require_once('validation.php'); // validation class
        
        
    class Base {
            
            function 
    Base() {
                
                
    // load extending classes
                
    $this->db = new Db();
                
    $this->validation = new Validation();
                
            }
            
            function 
    test() {
                
                
    print_r($this->validation->run());
                
            }
        }
        
        class 
    Db extends base {
            
            function 
    Db() {
                
            }
            
            function 
    query($sql) {
                
    //
            
    }
        }
        
        class 
    Validation extends base {
            
            function 
    Validation() {
                
            }
            
            function 
    run() {
                
                
    $this->db->query("SELECT * FROM `table`");
                
            }
        }
    What I want to be able to do, is call a sub function from another sub function.

    for example:
    PHP Code:
    <?php

    // load main class, and my two sub classes
    $instance = new Base();

    // fire a function which has access to my validation class, and my DB class.
    $instance->test();
    however I get the PHP Error

    Fatal error: Call to a member function query() on a non-object

    I have tried using the parent:: tag at the top of my subclasses like below, but that didn't seam to help;
    PHP Code:
        class Db extends base {
            
            function 
    Db() {
                
    parent::System();
            } 

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,244
    It sort of sounds to me like the Validation class is trying to call the query() method in Db. If so, Validation either needs to create its own Db object, or else you need to pass a reference to it as part of Validation's constructor.
    "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