Let's take this as example, I have a method call validateDateTime in a Validator class. This function is just simple as to check if the date time is in the required format.
namespace MyApp\Util;
use \DateTime;
class Validator {
public static function validateDateTime($dateTime, $format = 'Y-m-d') {
$d = DateTime::createFromFormat($format, $dateTime);
return $d && $d->format($format) == $dateTime;
}
}
Before I learned Symfony, I always use static method for the ease of the use of function if the function has to be shared across the application.
After I have learned Symfony, I know that Symfony has a very power full feature which is service container to perform the same convenience to access the function that will be used across entire application.
My questions are:
Your comment and opinion are very much appreciated.
Don't use static methods if you may need -in future- another implementation of the functionality you're writing. Static method are meant to be "static", not to be changed or extended.
Service "IoC" container is used to make your code easier to be changed or extended by decoupling components and inversing dependability between them, you can add new implementations or change existing ones without the need to do any changes to any dependent components.