简体   繁体   中英

Troubleshooting “Using $this when not in object context” when trying to access property of child class from parent class

I have a main class which initializes another class in a function of main class and I need to get a variable (in this case $this->db ) from the main class to that another class. For example, I have a main class Wordle_Admin :

class Wordle_Admin
{
   public function __construct()
   {
       $this->db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
   }

   public function views()
   {
       include 'Wordle_Admin_Views.php';
       $wsa = new Wordle_Admin_Views();
       $view = @$_GET['view'];
       $id = @$_GET['id'];
       $type = @$_GET['type'];
       $action = @$_GET['action'];
       $msg = @$_GET['msg'];

       if( !empty( $view ) && method_exists( $wsa, $view ) ):
           $wsa::$view($id, $type, $action, $msg);
       elseif( !empty( $view ) && !method_exists( $wsa, $view ) ):
           echo'View does not exist.';
       else:
           $wsa::index($id, $type, $action, $msg);
       endif;
   }    

}

And I have another class:

class Wordle_Admin_Views extends Wordle_Admin
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index( $id = "", $type = "", $action = "", $msg = "" )
    {
        $this->db->query('SELECT * FROM wordle_post');
    }
}

I can not use $this->db in Wordle_Admin_Views , it returns:

Fatal error: Using $this when not in object context in C:\\Server\\wordle\\w-includes\\Wordle_Admin_Views.php on line 22

So my question is, how can I pass a variable from parent class to class? All kind of help is appreciated, pointers on what to do differently are also accepted with open hands.

Your concrete problem is that you're calling the method statically. You don't have an object in a static context, so you can't use $this .

Your other problem seems to be that you're slightly misunderstanding inheritance. Since the DB connection is only created when you instantiate an object ( new Worlde_Admin ), the same goes for the child.

A Wordle_Admin_Views does not automatically have access to a DB connection that may or may not have been created in some instance of Worlde_Admin . Only that Worlde_Admin instance has that DB connection. You need to instantiate a Wordle_Admin_Views so it will have a connection as well (because it inherited the methods how to do so from Worlde_Admin ).

$admin  = new Worlde_Admin;  // $admin now has its own db connection
$admin2 = new Worlde_Admin;  // $admin2 now has another db connection
$view   = new Wordle_Admin_Views;  // $view has yet another connection

Wordle_Admin_Views::index();  // this has no db connection

You should be looking into Dependency Injection, which means you create one mysqli instance outside your classes and inject that into each one when it is instantiated.

$db = new mysqli(...);

$admin = new Worlde_Admin($db);
$view  = new Wordle_Admin_Views($db);

in your world admin class try this...

class Wordle_Admin
{
    protected $db;

}

then your db var should be inherited when you call $this->db, assuming $this->db was defined in the parent class.

It seems to me that this might be a part of issue:

   include 'Wordle_Admin_Views.php';
   $wsa = new Wordle_Admin_Views();

   /* blah blah */

   if( !empty( $view ) && method_exists( $wsa, $view ) ):
       $wsa::$view($id, $type, $action, $msg);
   elseif( !empty( $view ) && !method_exists( $wsa, $view ) ):
       echo'View does not exist.';
   else:
       $wsa::index($id, $type, $action, $msg);
   endif;

You are trying to access static function not on a class name but on object. It should be either Wordle_Admin_Views::$view() or $wsa->$view() .

Have you actually learned php or are you pretending that it is some other language (python ?) and just "winging it".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM