Search code examples
classneo4jautoloadneo4jphp

neo4jphp: Cannot instantiate abstract class Everyman\Neo4j\Transport


maybe a simple question but for me as starter with Neo4j a hurdle. I installed the neo4jphp with composer in the same directory as my application. Vendor-Subfolder has been created and the everyman/neo4j folder below is available. For a first test I used this code snippet from the examples:

spl_autoload_register(function ($className) {

  $libPath = 'vendor\\';  
  $classFile = $className.'.php';
  $classPath = $libPath.$classFile;
  if (file_exists($classPath)) {
    require($classPath);
  }
});

require('vendor/autoload.php'); 

use everyman\Neo4j\Client,
    everyman\Neo4j\Transport;

$client = new Client(new Transport('localhost', 7474));
print_r($client->getServerInfo()); 

I always stumple upon the error

Fatal error: Cannot instantiate abstract class Everyman\Neo4j\Transport

Googling brought me to a comment from Josh Adell stating

You can't instantiate Everyman\Neo4j\Transport, since it is an abstract class. You must instantiate Everyman\Neo4j\Transport\Curl or Everyman\Neo4j\Transport\Stream depending on your needs

So I thought I just need to alter the use-statements to

use everyman\Neo4j\Client,
    everyman\Neo4j\Transport\Curl;

but this doesnt work, debugging shows, that the autoloader only get "Transport.php" instead of "everyman\Neo4j\Transport\Curl.php". For "Client.php" its still working ("vendor\everyman\Neo4j\Client.php") so I am guessing that the use-statement is wrong or the code is not able to handle an additional subfolder-structure.

Using

require('phar://neo4jphp.phar');

works fine but I read that this is deprecated and should be replaced by composer / autoload.

Anyone has a hint what to change or had the same problem?

Thanks for your time, Balael


Solution

  • Thanks Josh, I was trying but it seems I still stuck somewhere. I am fine with using the default CURL - so I shrinked the code down to

    require('vendor/autoload.php'); 
       use everyman\Neo4j\Client;
        $client = new Everyman\Neo4j\Client('localhost', 7474);
        print_r($client->getServerInfo());` 
    

    The folder structure is main (here are the files and the composer.json with the content

    {
       "require": {
          "everyman/Neo4j": "dev-master"
        }
    }
    

    and in the subfolder "vendor" we have the "autoload.php" and the subfolder everyman with the related content. When I run the file I come out with

    Fatal error: Class 'Everyman\Neo4j\Client' not found 
    

    which does not happen when I have the autoloadfunction. I guess I made a mistake somewehere - can you give me a hint? Thanks a lot, B