如何摆脱这种循环依赖关系?

时间:2022-06-03 11:58:56

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

我目前正在编写一些类来处理PHP web应用程序中的本地化问题。

The classes are:

类:

  • Locale - Deals with setting and getting the user's locale, timezone, language.
  • 区域-处理设置和获取用户的语言环境、时区、语言。
  • LocaleFormat - Deals with formatting dates, collations, currency formats, etc.
  • LocaleFormat -处理格式化日期,排序,货币格式,等等。
  • 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.
  • LocaleData——获取本地化数据,例如地址格式和诸如post代码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.

这导致了这个问题:语言环境要求时区的方法,需要LocaleData的方法,这需要Locale的方法。

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 :)

欢呼:)

1 个解决方案

#1


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.

如果您只是从其他类调用方法,那么首先加载所有类,然后可以在类之间调用方法。不要在类文件中执行任何初始化,应该在单独的“加载器”文件中执行,该文件“包含”文件,然后调用do初始化。

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

如果您正在获得基于扩展其他类的类的循环依赖项,那么您需要重新考虑整个设置。

#1


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.

如果您只是从其他类调用方法,那么首先加载所有类,然后可以在类之间调用方法。不要在类文件中执行任何初始化,应该在单独的“加载器”文件中执行,该文件“包含”文件,然后调用do初始化。

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

如果您正在获得基于扩展其他类的类的循环依赖项,那么您需要重新考虑整个设置。