Welcome Guest, Not a member yet? Register   Sign In
'Class X not found' error
#1
Sad 

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?
Simpler is always better
Reply
#2

Try $db = new \PDO ...

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

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.
Simpler is always better
Reply
#4

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;
Reply
#5

(This post was last modified: 04-24-2017, 03:56 AM by sv3tli0.)

(04-22-2017, 09: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.
Best VPS Hosting : Digital Ocean
Reply




Theme © iAndrew 2016 - Forum software by © MyBB