Jump to content

Recommended Posts

Hi guys! I have a specific requirement and need some help

Say i have an external api library with Client class looking like this:

class Client {
	some constants..
	private props..,

	__construct

	public function create($url) {
		some code
	}

	public function authenticate($token) {
		some code
	}

	...dozen other methods
}

Current way of instantiating in every single other class where i need the client:

use Client;

MyFetchController
{
  private $client;

  public function __construct() {
      $this->client = Client::create(getenv('URL'))->authenticate(getenv('TOKEN'))
  }

	$this->client->use other methods in Client class
}

Is there a way to wrap that class so i have external parameters loaded through .env file automaticaly available on new wrapper class intialization with something like this:

MyFetchController
{
	private $client;

	public function __construct(WrappedClient $client) {
		$this->client = $client
	}

	public function getData() {
		$data = $this->client->parse(...)
	}
}

Basically what i need is a class ready for DI in all other classes without inserting required auth and other parameters in constructor every single time

Link to comment
https://forums.phpfreaks.com/topic/310428-wrapping-external-class/
Share on other sites

  • Barand locked this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.