ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

PHP Classes help

November 28, 2007 4:23pm

Subscribe [0]
  • #1 / Nov 28, 2007 4:23pm

    manilodisan

    223 posts

    I think I’m too tired for this. Need a fresh mind.

    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 ();
        }
    }

    When I do this I get an error that $class is not an object (call to a member function on a non object) at this line:

    echo $class->get_records ();

    Any idea why?

    P.S. This has nothing to do with CI

  • #2 / Nov 28, 2007 5:11pm

    tonanbarbarian

    650 posts

    There are 2 problems here that i can see

    the global in the class declaration is not possible I dont think
    if you move that into the function C it would be ok, except for the other issue.

    that because C is instantiated in the constructor of A, technically A does not fully exist yet. So the global variable will not exist and $class->get_records() in the constructor of C will not work.

    I think

  • #3 / Nov 29, 2007 6:20am

    ELRafael

    274 posts

    :shut:

    class A
    {
      function A()
      {
        include('db.php');
        $this->db = new db();
      }
      function get_records()
      {
        return $this->db->RecordCount('...');
      }
    }
    
    class C extends A
    {
      function show_records()
      {
        echo parent::get_records();
      }
    }
    
    $c = new C();
    $c->show_records();
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases