0

I should get the available products list and their prices from another server by WSDL (and NuSOAP).

No views is needed (and no controllers I think); So I create a model with no tables (because I don't want to store server data)

And use App:import('Vendor', 'path_to_nusoap.php') at the beginning of my model file.

Let's see my model:

<?php
App::uses('AppModel', 'Model');
App::import('Vendor', 'nusoap' . DS . 'nusoap.php');
/**
 * MyModel Model
 *
 */
class MyModel extends AppModel {
    public $useTable = false;

    public $client = new nusoap_client('url', 'WSDL');

    public function products(){
        $products = $client->call('getProductsList');
        ////
        return $products;
    }
    public function prices(){
        $prices = $client->call('getPricesList');
        ////
        return $prices;
    }
}

but it causes an error (on that line: public $client)

Now, the questions:

  1. How to solve that error? (use a contractor function?)
  2. Am I wrong to use this functions on model? (instead of controller)

Sorry for my terrible English. Thanks.

mrdaliri
  • 6,594
  • 21
  • 67
  • 101

1 Answers1

0

you cannot create an object outside of a method scope!

use a constructor:

public $Client;

public function __construct() {
    $this->Client = new nusoap_client('url', 'WSDL');

}

public function products() {
    $products = $this->Client->call('getProductsList');
    return $products;
}
mark
  • 21,478
  • 3
  • 46
  • 67