Thursday, 15 May 2014

php - Dependency Injection vs Static -


I am learning Symphony Framework and I am thinking: if I want something like a helper (for example) it Better is better to create a service (a dependency injection in my controller) or to create a stable work

P> This is a very important question which is the best way to add reusable libraries which are very specific. The Riyaan.

Symphony makes it a service and registers it in the service container.

  & lt ;? Php namespace Acme \ MainBundle \ Services; Class MobileHelper {public function formatMobile ($ number) {$ ddd = substr ($ number, 0, 2); $ Prefix_end_index = strlen ($ number) == 11? 5: 4; $ Prefix = substr ($ number, 2, $ prefix_end_index); $ Suffix = subtracut ($ number, -4, 4); Return sprintoff ('(% s)% s-% s', $ ddd, $ prefix, $ suffix); } Public function unformatMobile ($ number) {$ number = preg_replace ('/ [() - \s] /', '', $ number); Return number $; }}  

then on services.yml

  mobile.helper: Class: Acme \ MainBandal \ Services \ Mobile Service  

Then you can use it in your controller:

  $ mobileHelper = $ this- & gt; Receive ('mobile.helper'); $ FormattedMobile = $ MobileHelper-> Format Mobile ('11 99 762020 ');  

No comments:

Post a Comment