0

I added a namespace to my user.php file, this causes an error to be displayed :

Fatal error: Uncaught Error: Class 'User' not found in /var/www/html/login.php:9 Stack trace: #0 {main} thrown in /var/www/html/login.php on line 9

I tried changing "new Database\Database();" to "new \PDO();" but that causes another error which I've been unable to resolve after spending hours on google, if anyone could help I'd much appreciate it, thank you.

user.php

<?php
namespace User;

// 'user' object
class User
{

login.php

<?php
include_once "config/core.php";
$page_title = "Login";
$require_login = false;
include_once "login_checker.php";
include_once "config/database.php";
include_once "objects/user.php";
include_once "libs/php/pw-hashing/passwordLib.php";
$database = new Database\Database();
$db = $database->getConnection();
$user = new User($db);
Tamara Koliada
  • 1,200
  • 2
  • 14
  • 31
  • 1
    What exactly is the problem? Since the class `User` is in the namespace `User`, it's fully qualified name is `User\User`. – tkausl Mar 21 '19 at 12:06
  • Read the [docs](https://secure.php.net/manual/en/language.namespaces.importing.php) – Xatenev Mar 21 '19 at 12:07
  • @Xatenev - I've read the docs, but couldn't find the solution – The_Linux_Box Mar 21 '19 at 12:08
  • @tkausl - the problem is the error being displayed as mentioned in the post above, thank you – The_Linux_Box Mar 21 '19 at 12:09
  • @The_Linux_Box The solution is either making use of the `use` statement to import the namespace (as described in the docs) or use the fully qualified name as described by tkausl above. – Xatenev Mar 21 '19 at 12:09
  • BTW this is no `psr-2 namespace issue` - Read [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) again, it has nothing to do with this issue at all, its a guide about coding style. – Xatenev Mar 21 '19 at 12:11
  • 1
    Possible duplicate of [How does the keyword "use" work in PHP and can I import classes with it?](https://stackoverflow.com/questions/10965454/how-does-the-keyword-use-work-in-php-and-can-i-import-classes-with-it) – Xatenev Mar 21 '19 at 12:13
  • are you including the autoload.php ? – Yassine CHABLI Mar 21 '19 at 12:51
  • @MohammedYassineCHABLI Why do you think OP uses an autoloader? – Xatenev Mar 21 '19 at 12:59

1 Answers1

-1
use User\User;
$user = new User($db);