Author: Manuel Lemos
Viewers: 228
Last month viewers: 1
Categories: PHP Tutorials, News
This is an important recommendation if you need to test PHP application code that can manipulate dates and times using the current system clock.
Read this short article to learn more about this PHP standards recommendation, see some code examples, and get a suggestion of when it will be good for you to follow this recommendation.
What is is the PHP Standards Recommendation 20 - PSR 20
PHP Standards Recommendation, also known as PSR, is the result of the work of the PHP Framework Interoperability Group (FIG). This is a group of PHP developers that focus on reaching an agreement about what can be common practices that other developers can adopt as standards.
This group has produced many specifications for different aspects of implementing PHP applications.
The PSR 20 is a specification for defining a class interface that returns the current time.
Why You Should Follow the PSR-20 to Get the System Clock Times and Dates
PHP already has built-in functions and classes to return the current system clock time and date. For instance, the time() function returns the system clock time in seconds.
These time and date function and classes are fine for implementing PHP applications.
The problem is that when you need to create tests to verify that your application code works well, the code that relies on the system time will use different time values each time the test code is run.
We can use mock classes that wrap around functions and classes that get the current time and date to overcome this issue. This way, we can use a type for testing purposes that uses the same system clock time regardless of when that class is called.
The PSR 20 defines the ClockInterface, so you can define classes that return a DateTimeImmutable object.
namespace Psr\Clock; interface ClockInterface { /** * Returns the current time as a DateTimeImmutable Object */ public function now(): \DateTimeImmutable; }
An actual implementation of this interface can return a DateTimeImmutable object with the current system clock date and time.
class Clock implements Psr\Clock\ClockInterface
{
public function now()
{
return new \DateTimeImmutable('now');
}
}
A mock implementation of this interface for you to use in your application tests can return a DateTimeImmutable object with a fixed date and time.
Using a mock class to get the system clock date and time, the results of your tests will not change every time you run your tests.
class MockClock implements Psr\Clock\ClockInterface { public function now() { return new \DateTimeImmutable('2023-01-01'); } }
When You Should Follow PSR-20
As the name says, PSR (PHP Standards Recommendation) is just a recommendation. You are not required to use it. You can use it when it makes sense.
Following this recommendation would be a new coding habit if you did not have the habit of using a class to get the system clock. Adopting a new habit is not easy. It takes time to adapt.
If you have existing projects that work well, changing your project code may take a lot of time to use this recommendation to get the system clock data and time.
Even with the best code refactoring tools, you can use PHP, and your project code may break due to unexpected side effects.
My suggestion is that you use this new recommendation only for new projects. If you agree with my advice, you will realize that you can adopt this recommendation smoothly with little or no risk of breaking other parts of your code.
In sum, consider following this and other new standards recommendations only for future projects.
You need to be a registered user or login to post a comment
1,616,675 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
No comments were submitted yet.