Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!