0

I've been a php programmer for a number of years, but am only now getting into OOP. I have two classes so far, Item and List (I'm just simplifying here, they're not the real class names.) All of my data is accessed via a web service using SOAP.

I'm not sure what the best way is to implement getting a SOAP client and using it in multiple classes. For example, the WSDL defines an addItem function and a getList function. I also need to send a token to the service for each transaction.

Do I need to define a new SoapClient inside every class? like so:

class Item {
  // item properties
  function addItem($itemName) {
     $client = new SoapClient('myfile.wsdl');
     $client->addItem($itemName);
     //service returns true or false
  }
}

class List {
  function getList($listName) {
     $client = new SoapClient('myfile.wsdl');
     return $client->getList($listName);
     //service returns array
  }
}

Or is there some way to create a new SoapClient outside of the individual classes and use the same client object in each one?

1
  • Your class should have a member variable of type SoapClient. Commented Jul 16, 2012 at 18:20

3 Answers 3

1

Pass it into the constructor with some dependency injection:

class Item {

  function __constructor(SoapClient &$s){
      $this->soap = $s;
  }
  // item properties
  function addItem($itemName) {
     $this->soap->addItem($itemName);
     //service returns true or false
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Dependency injection is definitely preferred, but what's the point of &$s? The SoapClient instance is already passed in by reference ...
@rdlowrey is it? I did not know that.
@KarolyHorvath where is that documented?
@Neal for all intents and purposes, yes (relevant manual entry):
So in this case, am I calling something like $s = new SoapClient() somewhere else, and then passing $s into the class constructor?
|
1

Depending on how you're using the SoapClient, you have a couple of options.

If you're going to use that SoapClient multiple times during the execution of the script (and it is thread safe?), you could use the singleton pattern to fetch an instance of the SoapClient. What this would allow you to do is to only create ONE instance of the object and then fetch it again each time you need it.

To fetch the object the code would look like.

$soapclass = $SoapClassSingleton::getInstance()

And the class behind would look something like

class SoapClassSingleton{
private instance;
public static function getInstance(){
    if(!self::instance){            
        self::$instance = new SoapClass();          
    }
    return self::$instance;
}

It's almost like having a "global" variable, but is only created if you need it and then the same object can be used by other classes. I use this design for logging in my applications.

You could also create an instance of the SoapClient and pass it into the the Class when you create it. See PHP's constructors for help.

Alternatively your classes could extend a class that contains the SoapController. The downside to doing this is that now you are tightly coupled with the superclass.

Comments

-1

Oh dear, there are so many possible ways to do this. If you want to keep it the exact SAME in ALL portions of the script... you could use a singleton class.

Access:

MySoapConnection::instance()->function_calls_here

Class:

class MySoapConnection {

  /* Singleton */
  private static $_instance;
  public static function instance()
  {
    if (! self::$_instance):
      self::$_instance = new self();
    endif;
    return self::$_instance;
  }


  function __constructor(SoapClient $s){
      $this->soap = $s;
  }
  // item properties
  function addItem($itemName) {
     $this->soap->addItem($itemName);
     //service returns true or false
  }
}

As the comments state, I haven't tested. Nor should you copy/paste this code. Just shows a Singleton example. Nothing more, Nothing less. PHP was not built for OOP, I love PHP, was adapted to use OOP, not built for it.

6 Comments

Ummmm Singletons are uuuugly.
I agree Neal, but in PHP.... OOP is ugly. However, Singleton gets the job done here. :)
Lol that is not true at all. Also this will fail you did not pass the SoapClient into the Singleton!
Psh. PHP OOP is fine as long as you don't go trying to do stupid stuff.
@Rastapopulous: Constructors don't return anything -- they just construct. As for taking a SoapClient, that's kinda a design decision; i'd say it should unless it's encapsulating all the functionality of establishing the connection and everything (and will never need a SoapClient that's set up differently). In which case you may have SRP issues waiting to cause you problems.
|

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.