Reputation: 12138
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?
Upvotes: 0
Views: 1497
Reputation: 927
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.
Upvotes: -1
Reputation: 146300
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
}
}
Upvotes: 1
Reputation: 910
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.
Upvotes: 1