0

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?

4
  • The console command: "bin/console debug:container LoggerInterface" will list which class is mapped to the LoggerInterface. In the specific case of LoggerInterface, there are actually a dozen or so mappings but if you select the first one you will see that the concrete class is Symfony\Bridge\Monolog\Logger. You could look under vendor/Symfony and examine the actual php file. An alternative approach is to use xdebug or to simple add dump(get_class($logger)); to get the actual class. Commented Nov 13, 2019 at 20:31
  • DateTime is a bit different as it is not an interface but an actual php class. You could probably find it's source code in the php source repository if you looked hard enough but you really just want to look in the php online manual. DateTime::format You won't get very far in php without being comfortable using the manual. Commented Nov 13, 2019 at 20:35
  • And going back to the logger, I think you might actually be asking how the Symfony dependency injection service container works. Kind of makes you wonder why these sorts of questions are not considered appropriate for Stack Overflow. Commented Nov 13, 2019 at 20:38
  • I have looked at the docs, which is also why I chose the logger example. It's that I cannot understand how the interface is connected to the actual class. Where this is specified. In the case of DateTime, even though this is a class, the format() method is empty. Sure, I can look at the manual to see how this works, but I prefer looking at the actual code. Finding this code is hard though. Commented Nov 13, 2019 at 20:58

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Do you know how this works for a class that is not an interface like DateTime? I know this is a native php class, but how is it possible the method is empty? The method must be referring to some other code (right?), but how... and where?
A native PHP class doesn't have PHP source code, so I'm not sure what file you're referring to in your question. It's written in C.
I should explain better. By native I mean part of the language itself. If you look at the class DateTime, there is a method called format(), but this method is empty, as if it was in an interface (which it is not). I am trying to find out how this works, but fail.
"but this method is empty" Again, I have no idea what file you're looking at. You can't look at the PHP source of the class DateTime because it does not exist. See the update to my answer.
That very last part of your answer was what I was looking for, sorry for not being clear enough.
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.