6

I am currently writing a few classes to deal with localization in a PHP web application.

The classes are:

  • Locale - Deals with setting and getting the user's locale, timezone, language.
  • LocaleFormat - Deals with formatting dates, collations, currency formats, etc.
  • Timezone - Deals with compiling a list of time zones for countries and other functions related to timezones.
  • LocaleData - Fetches locale data, for example address formats and things like post code regexes.

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

This results in this problem: Locale requires Timezone's methods which requires LocaleData's methods which requires Locale's methods.

How can I break this circular dependency? Should I break my classes down into smaller pieces? Are there any patterns for dealing with this?

Cheers :)

F21
  • 32,163
  • 26
  • 99
  • 170

1 Answers1

2

If you are only calling methods from other classes, load all the classes first, then you can call the methods between the classes. Don't perform any initialization within the class files, that should be done in a separate "loader" file that "includes" the files, then calls the does initialization.

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

Brent Baisley
  • 12,641
  • 2
  • 26
  • 39
  • The issue I was facing was due to classes calling method in each other. Since I am using constructor injection using a DI framework, the dependencies are instantiated by the DI framework. The solution is that there's a method in Timezone that behaves more like a formatter, so I just moved it into LocaleFormat. – F21 Sep 18 '11 at 00:56