PHP中的数据封装和数据流 [英] Data Encapsulation and Data Flow in PHP

查看:310
本文介绍了PHP中的数据封装和数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重建我运行的提供潮汐表的网站。我正在使用Zend框架,并试图使其尽可能面向对象。我一直在考虑一个访问者从一个位置请求一个潮汐表以及如何返回的流程/过程。



我已经想出了两个不同的类型使用的对象将有助于此过程。第一个基本上是Zend Framework使用的帮助器对象。第二个是数据对象(由于缺乏更好的词)来封装在助手之间传递的数据。以下是我正在考虑的流程的粗略概念:




  • RequestHandler

    • 接收来自站点控制器或api控制器的请求的数组。

    • 创建名为 QueryData 的数据对象,并使用所有信息填充关于请求。

    • QueryData 对象传递给 LocationHandler

    • 返回 ResponseData 对象,它是从 LocationHandler 返回的。


  • LocationHandler

    • RequestHandler
    • 接收 QueryData 对象
    • 查找位置并创建一个 LocationData 对象来存储它。

    • 传递 QueryData LocationData 对象到不同的助手,如 TideHandler WeatherHan dler ,返回初始查询的特定数据。

    • 返回一组 ResponseData 对象,其中包含从每个特定的帮助者(TideHandler,WeatherHandler等)


  • TideHandler

    • LocationHandler 接收 QueryData LocationData 对象。

    • 使用数据对象查找潮汐数据。创建一个 ResponseData 对象以存储它。

    • ResponseData 返回到 LocationHandler




在这样做的过程中,我得到一个即插即用的OOP方法,添加到这很容易(我想?)。对不起,长的解释导致我的问题...



问题:



通常的做法是将数据集封装到一个对象(而不是一个数组)中,以传递给其上执行函数的其他对象并发送新的/修改的对象?有什么其他解决方案或模式提供类似的功能和灵活性?

解决方案

我想你在做什么是完美的尽管记住,您的数据对象只是对象 - 因此您可能会发现将这些对象或其父对象中的一些常见方法构建起来非常方便。对于我自己,我会说,如果你需要的是数据本身传递,然后使用一个数组。如果要将数据存储在能够处理或以其他方式处理数据的东西中,请使用对象:

  MyData类{
protected $ data = array();

public function __construct($ in_data)
{
$ this-> data = self :: prepare_data($ in_data);
}

保护静态函数prepare_data($ data)
{
//对数据执行某些操作
}
}

$ myData = new MyData($ _ REQUEST ['data']);
$ myData-> GetTideData(); //等等

常见做法真的只是风格 - 如果一种方法(如传递数据对象)对你来说更好,做到这一点。如果一个阵列更适合你的情况,那就去吧。


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?

解决方案

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.

这篇关于PHP中的数据封装和数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆