Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:38:20+00:00 2026-06-13T12:38:20+00:00

I have class Check { public function __construct() { $this->db = new Database(); }

  • 0

I have

class Check
{
    public function __construct()
    {
        $this->db = new Database();
    }

    public function query()
    {
        $login = Session::get("login");
        $sth = $this->db->prepare('SELECT admin FROM accounts WHERE login=:login');
        $sth->execute(array(':login' => $login));
        $result = $sth->fetch(PDO::FETCH_NUM);
        return $result[0];
    }

    public static function admin()
    {
        echo self::query();
    }
}

I have Database class in another place with PDO connection.

class Database extends PDO
{
    public function __construct()
    {
        parent::__construct('mysql:host=localhost;dbname=name','root','pass');
        $this->query('SET NAMES utf8');
    }
}

So after Check::admin() code I get error:

Undefined property: View::$db

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:38:21+00:00Added an answer on June 13, 2026 at 12:38 pm

    You are using a static method, that wants to use a instance variable.

    Your admin method calls the query method, and the query method is using the db instance variable. As your class is not instantiated, the db variable does not exists.

    My suggestion would be to make the admin method non static and use your code like this:

    $mycheck = new Check();
    $mycheck->admin();
    

    or, if you are on php 5.4 and want to stick with a oneliner:

    (new Check())->admin();
    

    update

    note: Do not create the db class in the constructor, but inject it:

    public function __construct(Database $db)
    {
       $this->db = $db;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one demo application to check select for update query public class Test
i have the following classes. class base { private function __construct(){ //check if foo
I have this code that will check for if the class more-results are in
I have a table structure like this: <table> <tr> <td> <ul> <li class=check></li> </ul>
<? if ( ! defined('BASEPATH')) exit(); class Registration extends CI_Controller { public function __construct()
I have this register that registers all the objects I need: public static class
I have class grades that I need to check if a specific grade is
I have a tempated base class check and publically derived class childcheck. the base
I have two objects of the same class (Model) type. I want to check
All, I have the following class so to check to see if the form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.