user4967821 user4967821 - 1 year ago 56
PHP Question

call a function in different namespacs php

I learn MVC recently. I try to rebulid my website in MVC structure, and i have a problem to call function inside different name sapace(maybe i don't know about OOP very much). Here is my code:

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);
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";

So basically i called
, then it shows this error:*
Call to undefined function UserFrosting\testFunction()*

I had same problem with
but i shows this error:*
Class 'UserFrosting\SoapClient' not found*
here is my
code in same namesapce:

public function templatePost(){

$client = SoapClient('', ['encoding' => 'UTF-8']);

$result = $client->PaymentRequest([
'MerchantID' => $MerchantID,
'Amount' => $Amount,
'Description' => $Description,
'Email' => $Email,
'Mobile' => $Mobile,
'CallbackURL' => $CallbackURL,

if ($result->Status == 100) {
} else {
echo'ERR: '.$result->Status;

I had the same error, what is my problem? how can i call these functions?

Answer Source

Explanation: You do not have a function testFunction, but the object GroupController has a method testFuntion. So you need to use $this->.. since you are already inside that class. Your loginNumber should read:

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";

In your second question you are not importing the class SoapClient correctly. Please try:

$client = \SoapClient(...);

This uses SoapClient from root namespace.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download