PHP:在OO中作为Object来处理JSON会更好吗?


PHP: Would dealing with JSON be better served as an Object in OO?

我了解PHP,但不太了解PHP中的OO。在Joomla工作时,我看到了使用OO和MVC的好处,并喜欢它们的表现方式。但我经常遇到这样的情况:我不知道这是否是OO的一个好选择。下面是一个火车时刻表的示例,作为print_r的JSON字符串输出。

为了在不使用PHP中的OO的情况下从时刻表中提取各种火车站点,我最终使用嵌套的'foreach'来获取火车站的出发和到达时间。

在OO环境中将其作为对象处理可能会更好吗?如果没有嵌套的"foreach"语句,是否可以更容易地获取数据,从而创建更优雅的编程环境?或者切换到OO并不能进一步简化这项任务和PHP代码的可读性?

如果你觉得面向对象是一种方式,如果你能给出一个例子,PHP将如何使用下面的例子来改善环境,这将是最有帮助的。谢谢!

stdClass Object
(
    [ITEM] => Array
        (
            [0] => stdClass Object
                (
                    [ITEM_INDEX] => 0
                    [SCHED_DEP_DATE] => 10:46:00 06/13/2013
                    [DESTINATION] => New York
                    [TRACK] => 2
                    [LINE] => AMTK
                    [TRAIN_ID] => A98
                    [STATUS] => ARRIVED
                    [BACKCOLOR] => yellow
                    [FORECOLOR] => black
                    [SHADOWCOLOR] => yellow
                    [GPSLATITUDE] => 40.7347
                    [GPSLONGITUDE] => -74.1644
                    [GPSTIME] => 6/13/2013 12:06:05 PM
                    [TRAIN_LINE] => Northeast Corridor Line
                    [STATION_POSITION] => 1
                    [LINEABBREVIATION] => AMTK
                    [INLINEMSG] => 
                    [STOPS] => stdClass Object
                        (
                            [STOP] => stdClass Object
                                (
                                    [NAME] => New York Penn Station
                                    [TIME] => 6/13/2013 12:26:11 PM
                                    [DROPOFF] => Discharge Only
                                )
                        )
                )
            [1] => stdClass Object
                (
                    [ITEM_INDEX] => 1
                    [SCHED_DEP_DATE] => 11:57:00 06/13/2013
                    [DESTINATION] => New York
                    [TRACK] => 1
                    [LINE] => AMTK
                    [TRAIN_ID] => A644
                    [STATUS] => in 2 Min
                    [BACKCOLOR] => yellow
                    [FORECOLOR] => black
                    [SHADOWCOLOR] => yellow
                    [GPSLATITUDE] => 
                    [GPSLONGITUDE] => 
                    [GPSTIME] => 6/13/2013 12:08:15 PM
                    [TRAIN_LINE] => Northeast Corridor Line
                    [STATION_POSITION] => 1
                    [LINEABBREVIATION] => AMTK
                    [INLINEMSG] => quiet car is in the rear.........thank you
                    [STOPS] => stdClass Object
                        (
                            [STOP] => stdClass Object
                                (
                                    [NAME] => New York Penn Station
                                    [TIME] => 6/13/2013 12:28:33 PM
                                )
                        )
                )
            [2] => stdClass Object
                (
                    [ITEM_INDEX] => 2
                    [SCHED_DEP_DATE] => 12:09:00 06/13/2013
                    [DESTINATION] => Raritan
                    [TRACK] => 5
                    [LINE] => RARV
                    [TRAIN_ID] => 5423
                    [STATUS] => ALL ABOARD
                    [BACKCOLOR] => Orange
                    [FORECOLOR] => white
                    [SHADOWCOLOR] => black
                    [GPSLATITUDE] => 
                    [GPSLONGITUDE] => 
                    [GPSTIME] => 6/13/2013 11:23:37 AM
                    [TRAIN_LINE] => Raritan Valley Line
                    [STATION_POSITION] => 0
                    [LINEABBREVIATION] => RARV
                    [INLINEMSG] => 
                    [STOPS] => stdClass Object
                        (
                            [STOP] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [NAME] => Union
                                            [TIME] => 6/13/2013 12:18:00 PM
                                        )
                                    [1] => stdClass Object
                                        (
                                            [NAME] => Roselle Park
                                            [TIME] => 6/13/2013 12:21:30 PM
                                        )
                                    [2] => stdClass Object
                                        (
                                            [NAME] => Cranford
                                            [TIME] => 6/13/2013 12:26:15 PM
                                        )
                                    [3] => stdClass Object
                                        (
                                            [NAME] => Westfield
                                            [TIME] => 6/13/2013 12:30:15 PM
                                        )
                                    [4] => stdClass Object
                                        (
                                            [NAME] => Fanwood
                                            [TIME] => 6/13/2013 12:34:30 PM
                                        )
                                    [5] => stdClass Object
                                        (
                                            [NAME] => Netherwood
                                            [TIME] => 6/13/2013 12:37:45 PM
                                        )
                                    [6] => stdClass Object
                                        (
                                            [NAME] => Plainfield
                                            [TIME] => 6/13/2013 12:40:45 PM
                                        )
                                    [7] => stdClass Object
                                        (
                                            [NAME] => Dunellen
                                            [TIME] => 6/13/2013 12:45:30 PM
                                        )
                                    [8] => stdClass Object
                                        (
                                            [NAME] => Bound Brook
                                            [TIME] => 6/13/2013 12:52:00 PM
                                        )
                                    [9] => stdClass Object
                                        (
                                            [NAME] => Bridgewater
                                            [TIME] => 6/13/2013 12:54:45 PM
                                        )
                                    [10] => stdClass Object
                                        (
                                            [NAME] => Somerville
                                            [TIME] => 6/13/2013 12:59:30 PM
                                        )
                                    [11] => stdClass Object
                                        (
                                            [NAME] => Raritan
                                            [TIME] => 6/13/2013 1:10:00 PM
                                        )
                                )
                        )
                )
            [3] => stdClass Object
                (
                    [ITEM_INDEX] => 3
                    [SCHED_DEP_DATE] => 12:12:00 06/13/2013
                    [DESTINATION] => NY Penn -SEC
                    [TRACK] => 1
                    [LINE] => NJCL
                    [TRAIN_ID] => 3240
                    [STATUS] =>  
                    [BACKCOLOR] => black
                    [FORECOLOR] => white
                    [SHADOWCOLOR] => 
                    [GPSLATITUDE] => 
                    [GPSLONGITUDE] => 
                    [GPSTIME] => 6/13/2013 11:47:35 AM
                    [TRAIN_LINE] => North Jersey Coast Line
                    [STATION_POSITION] => 1
                    [LINEABBREVIATION] => NJCL
                    [INLINEMSG] => 
                    [STOPS] => stdClass Object
                        (
                            [STOP] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [NAME] => Secaucus Upper Lvl
                                            [TIME] => 6/13/2013 12:19:30 PM
                                        )
                                    [1] => stdClass Object
                                        (
                                            [NAME] => New York Penn Station
                                            [TIME] => 6/13/2013 12:49:00 PM
                                        )
                                )
                        )
                )
            [4] => stdClass Object
... etc.

我试图避免或找到一个更好的方法,使PHP代码看起来不像这样:

foreach ($destinations->ITEM as $key=>$destination) {
    echo $destination->DESTINATION . "'t't";
    echo $destination->SCHED_DEP_DATE . "'n";
        foreach ($destination->STOPS as $key_stops=>$value_stops) {
            foreach ($value_stops as $key_stop=>$value_stop) {
                echo $value_stop->NAME . "'t";
                echo $value_stop->TIME . "'n";
            }
        }
    echo "'n";
    }

一些更直接的,也许像这样:$arrival_time = $train_destination($station->$lastrongtop);

所以也许函数被建议是更好的方法。

OOP非常适合将数据与行为联系起来。到目前为止,您所提到的都是需要访问数据。除非你需要这些功能,否则我不会用对象的行为来膨胀你的数据。为了避免代码中的嵌套循环,我会将查询数据的逻辑移到数据库中(假设您有一个);数据库比命令式代码更擅长这类事情,不管它是不是面向对象的。

考虑这样分解你的代码:

function printDestinationStop($stop) {
    echo $stop->NAME . "'t";
    echo $stop->TIME . "'n";
}
function printDestination($destination) {
    echo $destination->DESTINATION . "'t't";
    echo $destination->SCHED_DEP_DATE . "'n";
    foreach ($destination->STOPS as $stop) {
        printDestinationStop($stop);
    }   
}
function printDestinations($destinations) {
    foreach ($destinations->ITEM as $destination) {
        printDestination($destination);
        echo "'n";
    }
}

这也使您可以打印任何特定的停止或目的地,而不必像给定的嵌套循环那样打印所有目的地,因为打印特定元素的代码与主循环是分开的。

更好的方法(尽管在这种情况下可能有些多余)是为打印提供一个回调函数,以便数组/对象的遍历始终相同,但根据打印位置的不同,可能有许多输出格式(有点像访问者模式)。