Reputation: 103
I am struggling to understand what arguments to use for a particular methode from a class in PHP.
$client = new Client();
$client->execute();
The execute()
methode requires an argument. Looking at the execute()
methode in the Client
class it seems linked (linked is probably not the right word here) to the RequestInterface
interface. Which contains two methodes: getMethod()
and getPath()
. How do I use the execute()
methode?
class Client
{
public function execute(RequestInterface $request)
{
//do stuff
}
}
interface RequestInterface
{
/**
* @return string
*/
public function getMethod();
/**
* @return string
*/
public function getPath();
}
Upvotes: 0
Views: 221
Reputation: 631
Further to my comment, that would look something like this. Class MyRequest
implements interface RequestInterface
. You can then inject method Client::execute()
with a RequestInterface
type object ($request
).
<?php
Class MyRequest implements RequestInterface
{
public function getMethod()
{
// TODO: Implement getMethod() method.
}
public function getPath()
{
// TODO: Implement getPath() method.
}
}
class Client
{
public function execute(RequestInterface $request)
{
return 0;
}
}
interface RequestInterface
{
/**
* @return string
*/
public function getMethod();
/**
* @return string
*/
public function getPath();
}
$request = new MyRequest();
$client = (new Client)->execute($request);
var_dump($client); //0
see it in action
Suggested reading https://www.php.net/manual/en/language.oop5.interfaces.php
Upvotes: 1