I can not make PDO work in a P APIP API that I am creating and I do not understand the reason
Here I leave a bit of my code
The route I enter is http://localhost/public/api/order
, which executes the following code, which executes the autoload of the classes of my project, and directs me the route to the function, this works well, there is no problem here
public / index.php
<?php
define("PROJECTPATH", dirname(__DIR__));
spl_autoload_register(function($class) {
$filename = PROJECTPATH . "/$class.php";
$filename_real = str_replace(['/',"\"],DIRECTORY_SEPARATOR,$filename);
include_once $filename_real;
});
use Core\Route;
Route::get('/order', 'UserController@index');
Then, execute the method index
of UserController
where I call the Model App\User
and its method find
App / Http / Controller / UserController.php
<?php
namespace App\Http\Controller;
use App\User;
class UserController {
function index() {
print_r(User::find(1));
}
}
The following is the User model that extends the Model class
App / User.php
<?php
namespace App;
use Core\Model;
class User extends Model {
public $fillable = [
'username', 'password',
];
public $hidden = [
'password',
];
}
Core / Model.php
<?php
namespace Core;
use Core\DB;
class Model extends DB {
public static function find($id) {
return $this->$Conn;
}
}
and finally, my class of connection to the database
Core / DB.php (formerly Database.php)
<?php
namespace Core;
class DB {
public $Conn;
function __construct() {
$this->Conn = $this->connect('mysql:host=127.0.0.1;dbname=siccas', "root", "root");
}
function connect($driver, $user, $pass){
try {
$connection = new PDO($driver, $user, $pass);
} catch (PDOException $e) {
print_r($e->getMessage());
die();
}
return $connection;
}
}
This is the error in the log
[Sun Jul 29 20:48:57.842655 2018] [:error] [pid 26386] [client ::1:47265] PHP Fatal error: Using $this when not in object context in /var/www/html/Core/Model.php on line 9
Ignore if the PHP opening keys are missing, I omit it so that the code could be displayed here, thanks in advance