D
D
Dubrovin2018-09-03 19:40:49
PHP
Dubrovin, 2018-09-03 19:40:49

How to implement pattern module in PHP?

Given:
1. The main class of the application (framework, app, etc.)
2. Classes of modules (separate parts) of the application (may be with static methods or not)
How, depending on the conditions in the main class, connect module classes and use their methods through the main class (so that the main class is a facade)? Are there any examples of implementing the module pattern in PHP?
Google didn't help unfortunately.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Nesmeyanov, 2018-09-03
@SerafimArts

This is called interfaces and autoloading.

A
Andrew, 2018-09-04
@AndreuChel

A "module" is a popular implementation of a pattern that encapsulates private information, state, and structure using closures. This is a good solution to hide the internal logic from prying eyes and do all the hard work exclusively through an interface that you define for use in other parts of your application.
This pattern is only applicable in languages ​​like JavaScript, because there is no real privacy and unlike some traditional languages, it has no access modifiers.
In traditional OOP languages, the behavior you want is implemented through the core OOP concepts of encapsulation, inheritance, and polymorphism.
Modularity is polymorphism when there is a base class (interface) that the facade operates on. Depending on the conditions, certain classes inherited (implementing the interface) from the base one are slipped into it.

D
Dubrovin, 2018-09-04
@Dubrovin

I basically found a solution. The head class Framework inherits from Singleton. Instances of individual classes (modules) are created in the init method of the Framework class through new and stored in the properties of the Framework class. Modules communicate with each other only through the framework from any part of the code, for example Framework::i()->file_system->get_contents( $file ). Thus, the modules do not know anything about each other. If a module should not be loaded under any conditions, then it is wrapped in a condition in the init method and an instance of this module is not created.
It is very strange that few people understand architecture and everyone gives answers in the form of "slogans" without specific code examples.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question