php - Fatal error: Uncaught Error: Class 'todoviewsDataBase' not found

I am usingcomposer
toautoload
my classes.
My Folder structure:
mytodo
- src
-app
IDb.php
-views
db.php
todos.php
- web
index.php
-composer.json
Trying to inheritDatabase
class fromtodos.php
__constructor()
. But giving this error!!!
todos.php
<?php
namespace todo\views;
use todo\app\IDb;
class Todo extends DataBase implements IDb{
public function __construct()
{
$newdb = new DataBase();
$connection = $newdb->connect();
}
}
composer.json
{
"name": "todo/website",
"description": "This is a TODO App",
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"todo\\": "src/"
}
},
"require": {
"php": ">=7.2.0"
}
}
db.php
<?php
namespace todo\views;
use todo\app\IDb;
abstract class DataBase implements IDb{
private $user;
private $host;
private $pass;
private $db;
protected $connection;
public function __construct()
{
$this->user = "root";
$this->host = "localhost";
$this->pass = "";
$this->db = "db_todos";
}
public function connect()
{
$connection = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
return $connection;
}
}
?>
DB Interface:
<?php
namespace todo\app;
interface IDb {
public function connect() : string;
}
Can't figure out the problem. Please help me in advance.
Answer
Solution:
This happens because of the faulty naming. PSR-4 autoloading implies that the filename matches the class name.
Solution: rename files according to class names:todos.php
becomesToDo.php
,db.php
becomesDataBase.php
.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function update() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.