在 Symfony2 中加载 php-extension - 连接到 SAP


Loading php-extension in Symfony2 - connecting to SAP

作为这里的第一个问题,我将从一些困难的事情开始!

我正在做一个Symfony项目,该项目可以与SAP链接以提取数据并进行一些报告。我使用了piersharding的sapnwrfc扩展,因为它是唯一可以用我的环境(Windows x86和php 5.6.8)编译的扩展之一。

第一步效果还不错:

  • 将 DLL 文件添加到 PHP/ext
  • 将 php-sapnwrfc-x86.dll 行添加到 php.ini
  • 它出现在 phpinfo() 上
  • 使用这种代码使其在 Symfony(独立 PHP 脚本)之外的示例文件中工作:

/测试.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);
         }
         ....
  • 所以我在 Symfony 项目中尝试了类似的东西,但出现了此错误:

    "试图从全局命名空间加载类"sapnwrfc"。 你忘了"使用"语句吗?

使用此代码:

/

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.');
        }    
    }
.....

当然,如果我删除 use 语句,它也不起作用......谷歌不是一个很大的帮助,因为这似乎很具体。你有想法吗?这是symfony和php扩展之间的兼容性问题,还是我必须通知Symfony将此类用作php扩展?还是在我的项目配置中的某个地方我做错了什么?

如果您想了解更多详情,请告诉我!

尝试以下操作:

use 'sapnwrfc;
use 'sapnwrfcConnectionException;
use 'sapnwrfcCallException;

您缺少',这导致 php 在当前命名空间中搜索这些类,这是Extensions 。(代码中的第一行。

此外,您需要保留您的类/异常。

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.');
    }    
}

希望对您有所帮助。