0

I'm trying to initialize an object in my index.php, however it often requires an argument so I can't initialize and use it.

I created an interface that is implemented by a MYSQL function, used in a "User" Model that executes a "search" function, after that I try to keep it in my index.php, but it requires arguments. What kind of arguments should I pass to perform the execution? I dont have any constructor function on Database file. Just a class MYSQL which i want to use who makes the connection with the function conectar() and it implements Interface.

<?php

namespace Source\Model;
use Source\Controller\DatabaseInterface;

class User 
{

    private $dbConnection;


    public function __construct(DatabaseInterface $dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }

    public function search($dbConnection)
    {
        $query="SELECT * from categoria";
        $queryrun=mysqli_query($dbConnection, $query);
        echo '<pre>';
        var_dump($queryrun);
        echo '</pre>';

    }


}

Index.php

<?php 

require __DIR__ . "./vendor/autoload.php";

use Source\Model\User;

$teste = new User();

  • I'd recommend looking into dependency injection to help you with this, but I also don't know if it will align with your current pattern. I'd also recommend reading through this [answer](https://stackoverflow.com/a/5864000/231316) which is very long, but one of the takeaways is that the model isn't necessarily one class/file/thing. A more common pattern these days is that the `User` class would hold "user stuff" like name, email, etc., and a "UserMapper" class would take care of the database stuff that is specific to the User. – Chris Haas Nov 21 '22 at 16:03
  • What is the error message, exactly? – PajuranCodes Nov 22 '22 at 07:03

1 Answers1

0

You don't really need a database wrapper! Though, here are two interesting resources:

You should really use PDO instead of MySqli.
Here is a very good PDO tutorial.

And here is an example using an instance of PDO instead of a database wrapper.

index.php

<?php

require __DIR__ . "./vendor/autoload.php";

use Source\Model\User;

// Create a new database connection, e.g. a PDO instance.
$dbConnection = new PDO(
    'mysql:host=localhost;port=3306;dbname=guest;charset=utf8', // DSN
    'abc', // username
    'def', // password
    [
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_PERSISTENT => false,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ], // connection options
);

// Pass the PDO instance to all objects that need it (like object User).
$user = new User($dbConnection);

// Get all categories.
$categories = $user->getAllCategories();

echo '<pre>' . var_dump($categories) . '</pre>';

User.php

<?php

namespace Source\Model;

use PDO;

class User {

    private $dbConnection;

    public function __construct(PDO $dbConnection) {
        $this->dbConnection = $dbConnection;
    }

    public function getAllCategories(): array {
        $sql = 'SELECT * FROM categoria';

        /*
         * Always prepare your sql statements to avoid SQL injections.
         * 
         * @link https://phpdelusions.net/pdo#prepared Prepared statements. Protection from SQL injections
         * @link https://phpdelusions.net/sql_injection The Hitchhiker's Guide to SQL Injection prevention
         */
        $statement = $this->connection->prepare($sql);
        $statement->execute();

        $categories = $statement->fetchAll(PDO::FETCH_ASSOC);

        return $categories;
    }

}
PajuranCodes
  • 303
  • 3
  • 12
  • 43