samedi 5 novembre 2022

What pattern should I use for this in PHP?

I am creating a number of classes that are going to be used to fetch data from various different APIs. These classes are referred to as Network classes. There are going to be networks like NA, NB, NC, ND, and so on. Every network will be handled by an individual class. Each class has a static property named $network_name and $network_description;

However, in some instances, I don't need different classes for some networks because the API is exactly the same. Only difference is a couple of parameters. So obviously, for something so subtle, I don't want to create individual classes for such networks. I load these networks using a method called get_networks() which returns an array of network class names. These classes are loaded later.

I thought about using generators by creating a single Iterable class for such a network which yields objects with some different values. However, the problem I had is that I wasn't able to store the objects returned by the generator. If the generator returned five objects with values 1, 2, 3, 4 and 5 and I stored all of these in an array, in the end all five of them were just a copy of the last one. I tried cloning the object returned but to no avail.

Aucun commentaire:

Enregistrer un commentaire