Loading php-extension in Symfony2 - connecting to SAP

938 views Asked by At

As a first question here, I'll start with something hard!

I am working on a Symfony project that can link with SAP to extract data and do some reporting. I used the sapnwrfc extension from piersharding, as it is one of the only that can be compiled with my environment (windows x86 and php 5.6.8).

The first steps worked not so badly :

  • add the dll files to php/ext
  • add the line php-sapnwrfc-x86.dll to php.ini
  • it appears on phpinfo()
  • make it work in a sample file outside of symfony (standalone php script) with this kind of code:

/test.php

 <? php
 require_once 'sap_config.php';
 require_once 'spyc.php';
 extension_loaded("sapnwrfc");
 global $SAP_CONFIG;
 class _
 {
     public function setUp() {
         global $SAP_CONFIG;
         $this->config = Spyc::YAMLLoad($SAP_CONFIG);
     }

     public function sapConnect() {
         try {
             $this->conn = new sapnwrfc($this->config);
         }
         ....
  • So I tried something similar in a Symfony project, but this error appears :

    "Attempted to load class "sapnwrfc" from the global namespace. Did you forget a "use" statement?"

with this code :

/src/myBundle/Extensions/SapConnection.php

<?php

namespace SE\InputBundle\Extensions;

use SE\InputBundle\Extensions\SapConfig;
use SE\InputBundle\Extensions\Spyc;
use SE\InputBundle\Entity\SAPRF;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

//the following doesn't seem to do much:
extension_loaded("sapnwrfc");

use sapnwrfc;
use sapnwrfcConnectionException;
use sapnwrfcCallException;

global $SAP_CONFIG;

class SapConnection
{
    public function setUp() {
        global $SAP_CONFIG;
        $this->config = Spyc::YAMLLoad($SAP_CONFIG);
        //the following displays "false"
        $alertpop = (class_exists("sapnwrfc")) ? "true" : "false";
        echo "<script type='text/javascript'>alert('$alertpop');</script>";
    }

    public function sapConnect() {
        try {
            // the error is triggered here
            $this->conn = new sapnwrfc($this->config);
        }
        catch (sapnwrfcConnectionException $e) {
            echo "Exception type: ".$e."<br />";
            echo "Exception key: ".$e->key."<br />";
            echo "Exception code: ".$e->code."<br />";
            echo "Exception message: ".$e->getMessage();
        }
        catch (Exception $e) {
            echo "Exception type: ".$e."\n";
            echo "Exception key: ".$e->key."\n";
            echo "Exception code: ".$e->code."\n";
            echo "Exception message: ".$e->getMessage();
            throw new Exception('Connection failed.');
        }    
    }

.....

and of course it doesn't work either if I remove the use statement... Google isn't a big help as this seems pretty specific. Do you have an idea? Would it be a compatibility issue between symfony and the php-extension, or do I have to notify Symfony to use this class as a php-extension? Or is it somewhere in my project config that I did something wrong?

Let me know if you would like more details!

1

There are 1 answers

3
web-nomad On

Try the following:

use \sapnwrfc;
use \sapnwrfcConnectionException;
use \sapnwrfcCallException;

You are missing \, which leads to php searching for these classes in the current namespace, which is Extensions. (The first line in your code.)

Also, you need to escpae your classes/exceptions.

public function sapConnect() {
    try {
        // the error is triggered here
        $this->conn = new \sapnwrfc($this->config);
    }
    catch (\sapnwrfcConnectionException $e) {
        echo "Exception type: ".$e."<br />";
        echo "Exception key: ".$e->key."<br />";
        echo "Exception code: ".$e->code."<br />";
        echo "Exception message: ".$e->getMessage();
    }
    catch (\Exception $e) {
        echo "Exception type: ".$e."\n";
        echo "Exception key: ".$e->key."\n";
        echo "Exception code: ".$e->code."\n";
        echo "Exception message: ".$e->getMessage();
        throw new \Exception('Connection failed.');
    }    
}

Hope it helps.