在不同的命名空间中调用函数 php
call a function in different namespacs php
最近在学习MVC。我尝试在 MVC 结构中重建我的网站,但我在不同名称空间中调用函数时遇到问题(也许我不太了解 OOP)。这是我的代码:
namespace UserFrosting;
class GroupController extends \UserFrosting\BaseController {
public function testFunction($params){
//Simple test function that i had error: Call to undefined function UserFrosting\testFunction()
$params['Password'] = $pw;
$params['JSON'] = 'Yes';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
if (curl_errno($curl)) $obj = (object) array('Result' => 'Error', 'Error' => curl_error($curl));
else if (empty($response)) $obj = (object) array('Result' => 'Error', 'Error' => 'Connection failed');
else $obj = json_decode($response);
curl_close($curl);
return $obj;
}
public function loginNumber(){
$params = array("Command" => "SystemStats");
$api = testFunction($params);
echo "<h3>Server Status</h3>\r\n";
if ($api -> Result == "Error") die("Error: " . $api -> Error);
echo "Logins: " . $api -> Logins . "<br/>\r\n";
}
}
所以基本上我调用了 loginNumber()
,然后它显示了这个错误:*
调用未定义函数 UserFrosting\testFunction()*
我遇到了与 SoupClient
相同的问题,但我显示了这个错误:*
Class 'UserFrosting\SoapClient' 未找到*
这是我在同一个命名空间中的 soapClient
代码:
public function templatePost(){
$client = SoapClient('https://www.test.com/pg/services/WebGate/wsdl', ['encoding' => 'UTF-8']);
$result = $client->PaymentRequest([
'MerchantID' => $MerchantID,
'Amount' => $Amount,
'Description' => $Description,
'Email' => $Email,
'Mobile' => $Mobile,
'CallbackURL' => $CallbackURL,
]);
if ($result->Status == 100) {
header('Location: https://www.test.com/pg/StartPay/'.$result->Authority);
} else {
echo'ERR: '.$result->Status;
}
}
我有同样的错误,我的问题是什么?我该如何调用这些函数?
说明:您没有函数 testFunction,但是对象 GroupController 有一个方法 testFuntion。所以你需要使用 $this->.. 因为你已经在 class 里面了。您的登录号码应为:
public function loginNumber(){
$params = array("Command" => "SystemStats");
$api = $this->testFunction($params);
echo "<h3>Server Status</h3>\r\n";
if ($api -> Result == "Error") die("Error: " . $api -> Error);
echo "Logins: " . $api -> Logins . "<br/>\r\n";
}
在你的第二个问题中,你没有正确导入 class SoapClient。请尝试:
$client = \SoapClient(...);
这使用根命名空间中的 SoapClient。
最近在学习MVC。我尝试在 MVC 结构中重建我的网站,但我在不同名称空间中调用函数时遇到问题(也许我不太了解 OOP)。这是我的代码:
namespace UserFrosting;
class GroupController extends \UserFrosting\BaseController {
public function testFunction($params){
//Simple test function that i had error: Call to undefined function UserFrosting\testFunction()
$params['Password'] = $pw;
$params['JSON'] = 'Yes';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
if (curl_errno($curl)) $obj = (object) array('Result' => 'Error', 'Error' => curl_error($curl));
else if (empty($response)) $obj = (object) array('Result' => 'Error', 'Error' => 'Connection failed');
else $obj = json_decode($response);
curl_close($curl);
return $obj;
}
public function loginNumber(){
$params = array("Command" => "SystemStats");
$api = testFunction($params);
echo "<h3>Server Status</h3>\r\n";
if ($api -> Result == "Error") die("Error: " . $api -> Error);
echo "Logins: " . $api -> Logins . "<br/>\r\n";
}
}
所以基本上我调用了 loginNumber()
,然后它显示了这个错误:*
调用未定义函数 UserFrosting\testFunction()*
我遇到了与 SoupClient
相同的问题,但我显示了这个错误:*
Class 'UserFrosting\SoapClient' 未找到*
这是我在同一个命名空间中的 soapClient
代码:
public function templatePost(){
$client = SoapClient('https://www.test.com/pg/services/WebGate/wsdl', ['encoding' => 'UTF-8']);
$result = $client->PaymentRequest([
'MerchantID' => $MerchantID,
'Amount' => $Amount,
'Description' => $Description,
'Email' => $Email,
'Mobile' => $Mobile,
'CallbackURL' => $CallbackURL,
]);
if ($result->Status == 100) {
header('Location: https://www.test.com/pg/StartPay/'.$result->Authority);
} else {
echo'ERR: '.$result->Status;
}
}
我有同样的错误,我的问题是什么?我该如何调用这些函数?
说明:您没有函数 testFunction,但是对象 GroupController 有一个方法 testFuntion。所以你需要使用 $this->.. 因为你已经在 class 里面了。您的登录号码应为:
public function loginNumber(){
$params = array("Command" => "SystemStats");
$api = $this->testFunction($params);
echo "<h3>Server Status</h3>\r\n";
if ($api -> Result == "Error") die("Error: " . $api -> Error);
echo "Logins: " . $api -> Logins . "<br/>\r\n";
}
在你的第二个问题中,你没有正确导入 class SoapClient。请尝试:
$client = \SoapClient(...);
这使用根命名空间中的 SoapClient。