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

I’m trying to implement a role-based access model (RBAC) which I got from https://sevvlor.com/post/2014/10/14/how-to-make-role-based-access-control-in-php/.

I’m getting 3 errors and I was unable to fix them.

This is my first time ever working with CLASS.

Strict Standards: Accessing static property User::$db as non static in C:\...\user.class.php on line 16 Notice: Undefined property: User::$db in C:\...\user.class.php on line 16 Fatal error: Call to a member function prepare() on a non-object in C:\...\user.class.php on line 16

This is how it should be used…
[php]session_start();
//Connect to the database etc…
include("…/protected/connect_db.php");
require_once “Role.class.php”;
require_once “User.class.php”;

$user = new User($_SESSION[“user_id”]);

if ($user->hasPermission(“permission”)){
//This user is allowed to do this
echo “Great, this user has permission to do this”;
}[/php]

In my opinion (others may differ), I would throw that tutorial in the trash. The reason it uses static methods and I think they are a bad way of learning OOP. Here’s an article that better explains it - https://kore-nordmann.de/blog/0103_static_considered_harmful.html

I would do it this way:

[php]class USER {
private $user_level = \NULL;

/* Old fashion setter method */
public function setUserLevel($level) {
$this->user->level = $level;
}
public function hasPermission() {
if ($this->level === ‘sysop’) {
return TRUE;
}
}
}[/php]

then to call it simply do this:

[php]$user = new User();
$user->setUserLevel(‘sysop’);

if ($user->hasPermission()) {
echo “Great, this user has permission to do this!”;
}[/php]

Of course this is a very simple simple way of doing it, but I am just trying to illustrate a point. You could then create an Abstract Class and put the user’s roles (methods) in that class (hasPermission() for example). HTH John
P.S. I have tested this, so there might be some bugs.

I agree with Strider, the connection should not be static. Using dependency injection, you just grab the connection from the class.

His user class has an error in it. You don’t use $this->db on a static method, which is one area that is throwing you off. Taking care of that should fix at least one of the other errors.

For reference, here is a database class I utilize that follows a Singleton pattern:
[php]class db extends \PDO
{

public $pdo = null;

public function __construct()
{
    $this->pdo = new PDO('mysql:host=localhost; dbname=sample', 'root');
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

public function getConnection(){
    if ( $this->pdo !== null )
      return $this->pdo;
    else 
        echo "Problem";
}

}[/php]

Hi guys, thank you for replying to my question.

In the time since first reply by strider I went ahead and started reading some object oriented guides maybe to better understand what you guys refer to.

Like I mentioned in my opening thread, Im very new to class aka object oriented I have always coded in a procedural approach for everything. So please bear with me.

Now I that u have done reading I also agree with strider because static are way to public for a db connection.

Astopecipher thank you for the class I will use it.

I will reply back when I have a chance to work on it.

Just FYI that is meant to be a base class. In my models that use it, they extend it for each table.

yea I know basically that is what i did. i included your class before i could inherit from it.

Getting this error.

Fatal error: Call to a member function prepare() on null i

looks like the constructor it’s not setting pdo variable.

How are you calling it?

Example usage:

User class
[php]<?php

namespace API;

require $_SERVER[‘DOCUMENT_ROOT’] . ‘/test/classes/db.php’;

class User extends \db
{
public function __construct()
{
parent::__construct();
$this->pdo = parent::getConnection();
}

/**
 *
 * @return Array
 */
public function getAllUsers()
{
    $sql = "SELECT username FROM Users";

    $stmt = $this->pdo->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll();
}

[/php]

Then you instantiate the User class and call the methods.

[php]$user = new User();
$results = $user->getAllUsers();
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service