# PDO Not Found in Class

**URL:** <https://discourse.slimframework.com/t/pdo-not-found-in-class/2240>\
**Category:** Questions\
**Created:** [February 14, 2018, 9:28pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240 "2018-02-14T21:28:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aponnath](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@aponnath](https://discourse.slimframework.com/u/aponnath)\
**Post date:** [February 14, 2018, 9:28pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/1 "2018-02-14T21:28:34Z")

</div>

I use the Slim Framework Skeleton and followed the user guide to get started. As part of guide i created a db container in the dependencies.php

```
    $container['db'] = function ($c) {
    $settings = $c->get('settings')['db'];
    $pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'],
        $settings['user'], $settings['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
    };

```

now i can call stuff like this on my routes page which works fine

```
`$result = $this->db->query("CALL user_info.`sp_user_login`('$userid', '$passwd')");`

```

but as this is not the best way i would like to move some db functions in to classes which i will call from my routes page like this

```
    $app->get('/test', function (Request $request, Response $response, array $args) {
    $blackObj = new Security\BlackList($this->db);
    $Success = $blackObj->check_token($_SERVER['REMOTE_ADDR']);
    echo $Success;
    });

```

the below code runs fine on the routes page but fails in my class

```
    public function check_token($ip)
    {
      $sth = $this->db->prepare('CALL `user_info`.`sp_ip_blacklist`(?)');
      $sth->bindParam(1, $IP, PDO::PARAM_STR, 20);
      $sth->execute();
      $todos = $sth->fetchAll();

      return count($todos);
     }

```

The error i am getting is

Type: Error Message:  
Class ‘Security\PDO’ not found File: /var/www/html/api/src/Security/BlackList.php  
Line: 46

So how do i make PDO known in my class, i inject the $this-\>db when calling my function but i guess thats not enough

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [February 14, 2018, 9:54pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/2 "2018-02-14T21:54:04Z")

</div>

Are you importing PDO in your class? Is it namespaced?

```php
use PDO;
// ...
$sth->bindParam(1, $IP, \PDO::PARAM_STR, 20);

```

---

<div class="post-metadata">

**Author:** ![aponnath](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@aponnath](https://discourse.slimframework.com/u/aponnath)\
**Post date:** [February 14, 2018, 11:11pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/3 "2018-02-14T23:11:05Z")

</div>

Nope i did not as i thought using the db which is a PDO object would be good enough. But yes adding use pdo to my class did the trick.

---

<div class="post-metadata">

**Author:** ![silentworks](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/silentworks/32/10_2.png) [@silentworks](https://discourse.slimframework.com/u/silentworks)\
**Post date:** [February 14, 2018, 11:49pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/4 "2018-02-14T23:49:45Z")

</div>

> [@aponnath](#):
>
> PDO::PARAM\_STR

You need use PDO because of the line above that you have in your class, or you can do `\PDO::PARAM_STR`, but this would mean doing it multiple places if you use a PDO constant anywhere else.

---

<div class="post-metadata">

**Author:** ![aponnath](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@aponnath](https://discourse.slimframework.com/u/aponnath)\
**Post date:** [February 15, 2018, 2:25am UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/5 "2018-02-15T02:25:14Z")

</div>

By adding use pdo I was able to use the PDO::PARM\_STR without the slash

Also is there a way to get access to the db which I created in the dependencies.php without injecting it in the call to the function ?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [February 15, 2018, 7:17pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/6 "2018-02-15T19:17:32Z")

</div>

Passing it to the class’s constructor from the container is the preferred way.

---

<div class="post-metadata">

**Author:** ![aponnath](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@aponnath](https://discourse.slimframework.com/u/aponnath)\
**Post date:** [February 15, 2018, 7:42pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/7 "2018-02-15T19:42:51Z")

</div>

Not to sound like a newbee to Slim OOP (which i am) can you provide a sample or link to one ?

This is how i do it right now hope thats what you mean …

```
$app->get('/black', function (Request $request, Response $response, array $args) {
$ipAddress = $request->getAttribute('ip_address');
$blackObj = new Security\BlackList($this->db);
$Success = $blackObj->check_ip_blacklist($ipAddress);
if ($Success == 1)
 {
  echo "user on black list";
 }
  else {
   echo "Your Ip ".$ipAddress." is not blacklisted";
 }
});

```

And my class look like this now

```
namespace Security;
use pdo;

final class BlackList
{
public $db;

public function __construct($db) {
	$this->db = $db;
}

public function check_token($ip)
{
  $sth = $this->db->prepare('CALL `user_info`.`sp_ip_blacklist`(?)');
  $sth->bindParam(1, $ip, PDO::PARAM_STR, 20);
  $sth->execute();
  $todos = $sth->fetchAll();

  return $todos;
}
```

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [February 15, 2018, 10:17pm UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/8 "2018-02-15T22:17:46Z")

</div>

You are pretty much doing it by passing `$this->db` to your class. So it is clear that your class needs the `db` in order to function. There is a good overview of this here [Dependency Injection Factories for Slim Controllers](https://akrabat.com/di-factories-for-slim-controllers/) with some code examples.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 16, 2018, 10:59am UTC](https://discourse.slimframework.com/t/pdo-not-found-in-class/2240/9 "2018-02-16T10:59:33Z")

</div>

> [@aponnath](#):
>
> use pdo;

Namespaces may be case-insensitive, but autoloaders most often do. Write `use PDO;` instead.
