API recommendations in PHP: functions or classes?

In my company, we have developed some applications. We need to create an API for one application (for example, application A) so that others can use it (their data).

Question: we have already developed PHP classes for application model A, if we want to create an API, we must:
- reuse these classes (too much functionality for the API, too heavy ...)
- create one PHP class with some basic functions that takes input and returns only the original values ​​(for example, strings, arrays ... NOT complex classes)
- create another set of PHP classes, simpler and designed only for use by an external application (so only for easy data retrieval)

Typically, the API is the second solution (which should also be used with PHP than as a web service, for example), but I think it's too bad that we made a complex and useful model model, then we break it just have functions, lines and arrays. The third seems to me a compromise, but my colleague insists that this is not an API. Too bad...

What do you think?

+3
source share
4 answers

3 . , API. : , , ( , API) , .

( , ). (, -), ( ).

+3

Facade, API.

0

, API . - - , - , , , . / , - , , XML JSON , - 2 , , .

"".

0
source

I would also say look at the Facade template. Create a Facade class set that offers only the functionality that is really needed for public access. These classes will probably use your current main classes.

It also gives you the advantage that if you change the core classes, the API does not have to be changed.

0
source

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


All Articles