PHP classes and inheritance

Discussion in 'PHP' started by amorph, Nov 28, 2007.

  1. #1
    Class A {
        function A () {//constructor
            include ( 'db.php' );
            $this->db = new DB();//responsible for database operations
            $this->C = new C();//when instanciated, should echo those records
        }
    
        function get_records () {//returns a number
             return $this->db->RecordCount ( 'SELECT * FROM bla bla' );
        }
    }
    
    $class = new A();
    
    Class C {
        global $class;
        function C () {//constructor
            echo $class->get_records ();
        }
    }
    PHP:
    When I do this I het an error that $class is not an object (call to a member function on a non object). Any idea why?
     
    amorph, Nov 28, 2007 IP
  2. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    that is a mess i dont think you understand inheritance etc. to be able to use methods from class A you need class C to extend Class A and why are you making an instance of the supposed child class C in the parent class A?
     
    bobb1589, Nov 28, 2007 IP
  3. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    Class A {
        function A () {//constructor
            include ( 'db.php' );
            $this->db = new DB();//responsible for database operations
        }
    
        function get_records () {//returns a number
             return $this->db->RecordCount ( 'SELECT * FROM bla bla' );
        }
    }
    
    Class C extends A{
        global $class;
        function C () {//constructor
            echo $class->get_records ();
        }
    }
    
    $class = new C();
    
    PHP:
    this will work or at least it should
     
    bobb1589, Nov 28, 2007 IP