• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'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;
Practical CodeIgniter 3  • CodeIgniter 4 Foundations - Coming Soon • Vulcan - CLI Tools for CI4
Reply

#5
(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.
Best VPS Hosting : Digital Ocean
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.