I am using this library and when I put on my OOP project, it gave me an error https://github.com/lincanbin/PHP-PDO-MySQL-Class
Notice: Undefined variable: DB in E:\Laragon\www\shop\include\category.php on line 13
Fatal error: Uncaught Error: Call to a member function query() on null in E:\Laragon\www\shop\include\category.php:13 Stack trace: #0 E:\Laragon\www\shop\admin\category.php(8): Category->getAllCategories() #1 {main} thrown in E:\Laragon\www\shop\include\category.php on line 13
on my category class
<?php
require_once('../vendor/autoload.php');
class Category {
function __construct()
{
$DB = new Db('localhost', '3306', 'shop', 'root', '');
}
public function getAllCategories()
{
$query = $DB->query("SELECT * FROM categories");
return $query;
}
...
}
and in my php frontend
<?php
require_once('../include/category.php');
$test = new Category();
var_dump($test->getAllCategories());
?>
$this->DBinstead of$DB. You need to define it first. Basic OOP pattern.