Help with PDO and POO in PHP, I get 500 error

1

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

    
asked by Anthony Medina 30.07.2018 в 02:28
source

1 answer

0

You have a conflict problem between static and not static . You want to currently use the connection from a static method of the daughter class Model doing this:

    public static function find($id) {
        return $this->$Conn;
    }

but member Conn can not be accessed from that static context because he is not static in the parent class.

Two solutions are possible:

Solution 1:

Leave class Database as you have it now:

class Database {
   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;
    }
}

Declare the find method as non-static, then you can access the member Conn of the parent class without problems using $this :

    public function find($id){
        print_r($this->Conn);
    }

Solution 2:

Rethink class Database :

  • declaring $Conn as static ;
  • assigning the value of $Conn in this mode self::$Conn = $this->connect(...parámetros...); in the constructor.

The class would look like this:

class Database {
   public  static $Conn;

   function __construct() {

        self::$Conn = $this->connect('mysql:host=127.0.0.1;dbname=siccas', "root", "root");
   }

   function connect(){
        try {
            $connection = new PDO($driver, $user, $pass);
        } catch (PDOException $e) {
            print_r($e->getMessage());
            die();
        }   
        return $connection;
    }

}

And in Model there should also be changes:

class Model extends Database {

    public static function find($id) {
        print_r(parent::$Conn); // Solo quiero ver que tiene la variable de conexion
    }

}

Note that $Conn is used by invoking the parent parent and putting the sign $ in front of it.

If find is static this will imply that it should be called using :: from the instance of class Model , something like this: $m::find(...);

  

See how the methods would be invoked both in solution 1, as in   Solution 2, at the end of the proof of concept, where it is commented    /*Probando la clase*/ .

Proof of concept

Here you can see a DEMONSTRATION of both solutions. The method only prints ok instead of returning the connection to PDO. It is the same, if you connect well, it will return your connection object.

    
answered by 30.07.2018 в 04:23