1

i have a problem when i am calling a remote methode using initilized variable as parameter then i get nothing in resutl, but when i pass a value as parameter everything work fine ! here is the code in php:

$serviceWsdl = 'http://localhost:8080/Test/services/Test?wsdl';
$client = new SoapClient($serviceWsdl);

function getFirstName($code){
    $firstname = $client->getFirstName(array('code' => $code));
    return $firstname->return;
}

$c=1;
$result=getFirstName($c);
var_dump($result);

1 Answer 1

1

You should read a bit about scopes in PHP. Your variable client is not set in your function because that is another scope. There are some solutions to handle that. You can get the variable with global but that is not really cool.

function getFirstName($code){
    global $client;
    $firstname = $client->getFirstName(array('code' => $code));
    return $firstname->return;
}

You shouldn't do that. When you work with globals you don't know where your variable come from.

Another solution is to define your variable as function parameter.

function getFirstName($code, $client) {

thats much better. If you work with classes you can define the variable as class variable thats much better. For example:

class ApiConnection {
    private $serviceWsdl = 'http://localhost:8080/Test/services/Test?wsdl';
    private $client;

    public function __construct() {
        $this->client = new SoapClient($this->serviceWsdl);
    }

    public function getFirstName($code){
        $firstname = $this->client->getFirstName(array('code' => $code));
        return $firstname->return;
    }
}

i haven't tested that code but its much better to work with classes.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you man ... the problem is resolved .. it's long time i did not code in php -_- ... sometimes simple things makes things looks hard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.