class BaseClient
{
protected $client = null;//declare your properties!!!
protected $config = null;
//use type hints, default = null means you don't HAVE to pass the argument
public function __construct(\SoapClient $client = null)
{
$this->client = $client;
}
//lazy-loading getter
protected function getClient()
{
if ($this->client === null)
{//set client only when it's required
$this->setClient(
new \SoapClient($this->config['wsdl'], $config['options']
);
}
return $this->client;
}
//public to allow injection
protected function setClient(\SoapClient $client)
{
$this->client = $client;
return $this;//makes your api chainable
}
}
Declare your properties
From the wrapper tag wiki:
A wrapper is an OOP technique where an object encapsulates (wraps) another object, hiding/protecting the object and controlling all access to it.
By not declaring $this->client
, client
is effectively added later on to your object, resulting in marginally slower code but more importantly: public properties. If $this->client
is public, then you don't have a wrapper because:
A wrapper [...] encapsulates another object, hiding/protecting the object and controlling all access to it.
Thus, $this->client
has to be protected or private.
Next.