0

As the name suggests, i'm having issues with my db class trying to autoload PDO.php

class DBObject extends PDO
{
    public function __construct( $config ) 
    {
        $conn = "mysql:host=" . $config['host'] . ";dbname=" . $config[ 'dbname' ];


        try
        {
            parent::__construct( $conn, $config['user'], $config['password'] );
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }  
        catch(PDOException $e) 
        {
            echo $e->getMessage();  
        }  
    }}

This works fine locally.

autoload function:

function autoload( $className ) 
{
    $filename = str_replace("_", DIRECTORY_SEPARATOR, $className ) . '.php';

    require_once( $filename );
}
spl_autoload_extensions('.php');

// Use default autoload implementation
spl_autoload_register("autoload");

nothing fancy.

The strange this is the front end of the site works - no errors, everything displays fine, all database accesses work, but when I go into the backend on the staging server ( a custom cms made by the company i work for, which uses mysql_* ) it tries to load PDO.php in the autoloader when I load my models.

The only thing that I can think is causing the problem, is that the classes that use the DBObject are instantiated inside a function, but even then, thats just a guess, and a long shot. There's nothing fancy happening, no magic methods, no namespaces, no other autoload function

as i'm stuck on php 5.2.7 there is no namespaces, so none of the other threads i found on here help.

according to phpinfo, PDO is installed, with PDO Driver for MySQL, client library version 5.0.96

any help would be greatly appreciated.

Simon Dobie
  • 59
  • 1
  • 10
  • 1
    Are you 100% sure PDO is being loaded on the staging server? Can you run phpinfo on the staging and see that PDO is loaded? Also, are you running this code via CLI or a webserver? – Mike Hancock Oct 02 '13 at 06:30
  • In order to catch an error, you'd enable `error_reporting(E_ALL)` at the top of the bootstrapper. – Yang Oct 02 '13 at 06:45
  • phpinfo has a pdo section, and it says its got the mysql driver. the error message i get from error_reporting is Warning: require_once(PDO.php) [function.require-once]: failed to open stream: No such file or directory in ... followed by Fatal error: require_once() [function.require]: Failed opening required 'PDO.php' (include_path='.:/usr/lib/php: – Simon Dobie Oct 02 '13 at 06:52
  • I would **never** advocate extending the PDO class. Instead, inject a PDO instance as a dependency where required – Phil Oct 02 '13 at 06:57
  • Off course, `require_once()` will issue a warning, because you don't supply correct path. Instead try the full path, like, `require_once(dirname(__FILE__) . '/PDO.php')` – Yang Oct 02 '13 at 07:03
  • @Phil Its okay to extend `PDO` if it adheres to the LSP and SRP at the same time. – Yang Oct 02 '13 at 07:07
  • @DaveJust There is no `PDO.php` file as the `PDO` class is meant to be included by the extension. Also, I have never seen a valid reason for extending PDO. – Phil Oct 02 '13 at 07:08
  • @Phil How about this? http://stackoverflow.com/a/18043078/1208233 – Yang Oct 02 '13 at 07:11
  • just changed the db method over so it doesnt extend PDO... same error. so confusing. – Simon Dobie Oct 02 '13 at 23:02
  • This question appears to be off-topic because it is too localised – Phil Oct 03 '13 at 00:11

1 Answers1

1

ok so i figured it out. there was a php.ini file in the cms directory, commenting out the entire file didnt work - i had to remove it and everything works as it does locally.

Simon Dobie
  • 59
  • 1
  • 10