Data Encapsulation and Data Flow in PHP

I am rebuilding a website that I am running that provides tide tables. I am doing this with the Zend Framework and trying to make it as objective as possible. I was thinking about the thread / process that will occur when a visitor requests a tide table by location and how it returns.

I came up with two different types for using objects that will help in this process. The first is basically a helper object that uses the Zend Framework. The second is a data object (due to the lack of a better word) for encapsulating data that is transferred between helpers. Here's a rough idea of โ€‹โ€‹the stream I'm thinking of:

  • RequestHandler
    • Gets the array for the request from the site controller or from the api controller.
    • Creates a "data object" called QueryData strong> and populates it with all the information about the query.
    • Skips the QueryData strong> object to the LocationHandler .
    • Returns the ResponseData strong> object that was returned to it from the LocationHandler .
  • Locationhandler
    • Gets a QueryData strong> object from RequestHandler
    • Searches for a location and creates a LocationData strong> object to store it.
    • Skips QueryData strong> and LocationData strong> objects for various helpers, such as TideHandler or WeatherHandler , that return specific data for the initial query.
    • Returns an array of ResponseData strong> objects that contains the responses returned to it from each specific helper (TideHandler, WeatherHandler, etc.).
  • Tidehandler
    • Retrieves QueryData strong> and LocationData strong> Objects from LocationHandler .
    • Does tidal data search work using data objects. Creates a ResponseData strong> object to save it.
    • Returns ResponseData strong> in LocationHandler .

By doing everything this way, I get the โ€œplug and playโ€ OOP approach, which allows me to add to this a lot easier (I think?). Sorry for the long explanation leading to my question ...

Question:

Is it common practice to encapsulate data sets in an object (rather than an array), which should be passed to other objects that perform functions on it and send new / changed objects? What are some other solutions or templates that provide the same level of functionality and flexibility?

0
source share
1 answer

I think what you are doing is great practice, but keep in mind that your Data objects are just objects - so you may find that creating some of the common methods in these objects or in their parent objects is quite convenient. For myself, I would say that if all you need is data to go through, then use an array. If you want to store data in something that has the ability to manipulate or otherwise work with data, use the object:

class MyData { protected $data = array(); public function __construct($in_data) { $this->data = self::prepare_data($in_data); } protected static function prepare_data($data) { // do something to the data } } $myData = new MyData($_REQUEST['data']); $myData->GetTideData(); // Etc. 

โ€œcommon practiceโ€ is really just a style โ€” if an approach (like passing data objects) works best for you, do it. If the array is better suited to your situation, go with that.

+1
source

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


All Articles