Reputation: 4701
This is just an example to illustrate my issue. Quite often, I am looking for (vendor) code that executes a method, but end up in the interface (which I guess is implemented by the code I am looking for).
In a Symfony app, I have a function Example()
that uses $logger
, which is typehinted with LoggerInterface
. I use the correct interface by adding to my file: use Psr\Log\LoggerInterface;
. In my function I use $logger
to log an error with the method error()
like so:
public function Example(LoggerInterface $logger)
{
$logger->error('There was some error.')
}
When I open the file that contains the LoggerInterface
I can see how this method:
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function error($message, array $context = array());
This can be useful for the documentation and for seeing which arguments one must pass.
My question: how do I find the method that does the actual work (i.e. that is being executed) and how does the $logger
'finds' this through the interface?
In a more generic PHP example, when I need a dateTime object I will use new DateTime();
(with a use statement: use DateTime;
). Then I could call the method format()
on it. When I want to find out exactly what format()
does, looking for it in the class DateTime
, all I find is:
/**
* Returns date formatted according to given format.
* @param string $format
* @return string
* @link https://php.net/manual/en/datetime.format.php
*/
public function format ($format) {}
Again I wonder: how to find the method doing the work?
Upvotes: 0
Views: 525
Reputation: 42762
Psr\Log\LoggerInterface
is, as its name suggests, an interface. This means that it specifies the methods an inheriting class must implement, but – as you've seen – it doesn't provide any code for those methods.
Any class written to implement Psr\Log\LoggerInterface
must include all the methods listed in the interface. Any such class will satisfy the type declaration in the method signature.
The actual class of your object can be determined by running get_class()
on the object in question, e.g.
public function Example(LoggerInterface $logger)
{
$logger->error("I am an instance of class " . get_class($logger));
}
With respect to native PHP classes, although the documentation may be written to look like they are standard PHP classes with methods and properties, this is not the case. They are written in C, which you can find in the repository.
Taking your example of DateTime::format()
, it's first defined as a method of the DateTimeInterface
. This is implemented by the DateTime
class, where it is simply specified as an alias to the date_format
method. Unless you're very good with C, I'd suggest sticking to the documentation
Upvotes: 2
Reputation: 121
Multi-tier question, deserves a multi-tier answer.
1. Configuration
Specifically for logger, it can be any logger that follows Psr\Log standards, but symfony is configured to use monolog. You can see detail on: https://symfony.com/doc/current/logging.html (disclaimer: I'm not symfony expert but this looks logical).
So - logger is whatever you configure.
2. Programming style
When you are programming - anywhere inside a function and want to know exact instance of logger (or any other object), you can call get_class() function and get a specific instance class name. Details on: https://secure.php.net/manual/en/function.get-class.php
3. Debugger
You can use Xdebug, add breakpoint to specific function and in variable view check instance class name.
4. PHP native objects
You can find exact functionality in PHP source code: https://github.com/php/php-src
Upvotes: 2