CodeIgniter Forums

Full Version: 'Class X not found' error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
Try $db = new \PDO ...

You have namespaced your class, so you should expect such things.
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.
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;
(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.