CodeIgniter Forums
'Class X not found' error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: 'Class X not found' error (/thread-67909.html)



'Class X not found' error - donpwinston - 04-22-2017

Code:
<?php namespace App\Libraries;

class BNCdb
{
    private $db = NULL;

    public function __construct()
    {
        try {
            $db = new PDO('informix:host=dev-bnc3-db; database=development; server=devbnc, bncdev, bncdev!');
         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } catch (PDOException $e) {
            log_message('error', $e->getMessage());
        }
    }
}

When I instantiate this class I get a 'Class App\Libraries\PDO not found' error. Why?

PDO is enabled. I'm using PHP 7. Why is it looking in App\Libraries?


RE: 'Class X not found' error - skunkbad - 04-22-2017

Try $db = new \PDO ...

You have namespaced your class, so you should expect such things.


RE: 'Class X not found' error - donpwinston - 04-22-2017

Smile

Thanks, It appears that sessions, redirects, validation, helpers, and libraries all work. I'm going to  use CI4 for my next project. Just got to get used to using namespaces.


RE: 'Class X not found' error - kilishan - 04-23-2017

You're forgetting about a how namespaces work. Since that file defined in namespace App\Libraries, all other classes will be assumed to be in that namespace unless other wise specified. Since you don't have anything to tell it otherwise, it's looking for PDO within the current namespace, App\Libraries. There are two solutions:

1. Prepend a backslash to PDO so it knows to look in the "root" namespace.

Code:
$db = new \PDO(...);

2. Tell it you're going to use that class at the top of file:

Code:
use PDO;



RE: 'Class X not found' error - sv3tli0 - 04-24-2017

(04-22-2017, 10:52 PM)donpwinston Wrote: Smile

Thanks, It appears that sessions, redirects, validation, helpers, and libraries all work. I'm going to  use CI4 for my next project. Just got to get used to using namespaces.

CI 4 is not yet even in beta .
Think twice before you do that.