Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
Symfony: Service Container VS Static Method

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:

  1. What are the pros and cons of using static method vs service container
  2. In Symfony best practice, is it recommended to use service container over static method if I want to achieve same purpose (Function that sharing across entire application).
  3. In what situation, when and why, we need to use static method

Your comment and opinion are very much appreciated.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda