Search code examples
phppostgresqlpomm

How do I load a PHP Class?


I use Pomm 1.1.4 and I cannot load the class. Here it says Fatal error: Class 'Pomm\Connection\Database' not found in /users/ilhanna/public_html/api/v1/Pomm-1.1.4/Pomm/Service.php on line 38 There is nothing on line 38, just a comment. My code to load the class is

require_once 'Pomm-1.1.4/Pomm/Service.php';
# Using the constructor
$service = new Pomm\Service(array(
  'db_one' => array(
    'dsn' => 'pgsql://username:password@localhost:5432/databasename'
  )
  ));

I am missing something I think.


Solution

  • I would suggest you use composer to install Pomm and get an autoloader. Download composer.phar from packagist

    create a composer.json file like this:

    {
    "require": {
        "pomm/pomm": "~1.1"
      }
    }
    

    and execute the phar to install Pomm. Just add the following code in your index.php

    $loader = require __DIR__."/vendors/autoload.php";
    

    If you cannot use composer, create your own autoload mechanism:

    spl_autoload_register(function ($class) {
        if (0 === strpos($class, 'Pomm\\')) {
            $class = str_replace('\\', '/', $class);
            require sprintf("%s/%s.php", __DIR__, $class);
    }
    

    at the very begining of your index.php file and it should run fine.