作为第一个问题,我将从一些事情开始!
我正在开发一个Symfony项目,该项目可以与SAP链接以提取数据并进行一些报告。我使用了piersharding的sapnwrfc扩展,因为它是可以用我的环境编译的唯一一个(windows x86和php 5.6.8)。
第一步工作并不是那么糟糕:
/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);
}
....
“尝试从全局命名空间加载类”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扩展?或者在我的项目配置中某处我做错了什么?
如果您想了解更多详情,请与我们联系!
答案 0 :(得分:2)
尝试以下方法:
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.');
}
}
希望它有所帮助。