neo4jphp: Cannot instantiate abstract class Everym

2019-09-06 21:33发布

问题:

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

回答1:

Curl is the default transport. You only need to instantiate your own Transport object if you want to use Stream instead of Curl. If you really want to instantiate your own Curl Transport, the easiest change to your existing code is to modify the use statement to be:

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

Also, you don't need to register your own autoload function if you are using the Composer package. vendor/autoload.php does that for you.



回答2:

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



回答3:

Hmmm... I was just trying around and it seems the Transport CLASS is not needed in the use-statement and the class instantiation. This seems to work:

require('vendor/autoload.php'); 

    use everyman\Neo4j\Client;

$client = new Client();
print_r($client->getServerInfo()); 

also valid for having a dedicated server/port:

$client = new Everyman\Neo4j\Client('localhost', 7474);

If you have more input I would be happy to learn more - thanks, all input & thoughts are very appreciated.

Balael