Data Encapsulation and Data Flow in PHP

2019-09-09 12:47发布

I am rebuilding a website I run which provides tide tables. I am doing it with the Zend Framework and am attempting to make it as object oriented as possible. I have been thinking about the flow/process that will occur when a visitor requests a tide table from a location and how it returns.

I have come up with 2 different "types" of objects to use which will assist in this process. The first is basically a helper object that Zend Framework uses. The second is a data object (for lack of a better word) to encapsulate data that is passed between helpers. Here is a rough idea of the flow I am thinking about:

  • RequestHandler
    • Receives array for request from site controller or from api controller.
    • Creates "data object" called QueryData and populates it with all information about the request.
    • Passes QueryData object to LocationHandler.
    • Returns the ResponseData object which was returned to it from the LocationHandler.
  • LocationHandler
    • Receives QueryData object from RequestHandler
    • Does work finding location and creates a LocationData object to store it in.
    • Passes both QueryData and LocationData objects to different helpers such as TideHandler or WeatherHandler which return specific data for the initial query.
    • Returns an array of ResponseData objects which contains the responses returned to it from each of the specific helpers (TideHandler,WeatherHandler,etc)
  • TideHandler
    • Receives QueryData and LocationData objects from LocationHandler.
    • Does work using data objects to find tide data. Creates a ResponseData object to store it in.
    • Returns ResponseData to LocationHandler.

In doing everything this way I get a "plug and play" OOP approach that allows me to add to this much easier (I think?). Sorry for the long explanation leading up to my question...

The Question:

Is it common practice to encapsulate sets of data into an object (rather than an array) to be passed around to other objects which perform functions on it and send on new/modified objects? What are some other solutions or patterns that provide a similar level of functionality and flexibility?

1条回答
该账号已被封号
2楼-- · 2019-09-09 12:52

I think what you are doing is perfectly fine practice, though keep in mind that your Data objects are just that - objects - so you may find that building some of the common methods into those objects or into their parent objects is quite handy. For myself, I'd say that if all you need is the data itself to pass around, then use an array. If you want to store the data in something that has the ability to manipulate or otherwise work on the data, use an 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 style - if an approach (such as passing data objects around) works better for you, do it. If an array better suits your situation, go with that.

查看更多
登录 后发表回答