How to get rid of this cyclic dependence?

I am currently writing several classes for working with localization in a PHP web application.

Classes:

  • Locale - works with settings and gets user language, time zone, language.
  • LocaleFormat - works with formatting dates, comparisons, currency formats, etc.
  • Time Zone - Deal with a list of time zones for countries and other time zone related functions.
  • LocaleData - Retrieves locale data, such as address formats and things like regular expressions of a postal code.

The whole application works correctly, but I need to add a few more things to Timezone.

This leads to this problem: Locale requires Timezone methods, which require LocaleData methods, which require Locale methods.

How can I break this cyclic dependency? Should I break my classes into smaller pieces? Are there any patterns to solve this problem?

Greetings :)

+6
source share
1 answer

If you only call methods from other classes, first load all classes, then you can call methods between classes. Do not initialize inside class files, which must be executed in a separate “bootloader” file, which “includes” the files and then calls initialization.

If you get circular dependencies based on classes extending other classes, you need to rethink the whole setup.

+2
source

Source: https://habr.com/ru/post/897514/


All Articles