PHP:处理JSON会更好地作为OO中的对象吗?

时间:2013-06-13 16:14:40

标签: php json oop

我知道PHP,但在PHP中使用OO并不多。在Joomla工作,我可以看到使用OO和MVC的好处,以及它出现的方式。但是我经常遇到这样的情况:我不知道这对于OO是否是一个好的候选人。以下是作为print_r的JSON字符串输出的列车时刻表示例。

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

在环境OO中将它作为对象处理可能会更好吗?如果没有所有嵌套的'foreach'语句来创建更优雅的编程环境,是否可以更轻松地获取数据?或者切换到OO不会进一步简化此任务和PHP代码的可读性吗?

如果您认为OO是可行的方法,那么您可以使用下面的示例举例说明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-> $ last_stop);

因此,建议的功能可能是更好的方法。

2 个答案:

答案 0 :(得分:1)

OOP非常适合用行为绑定数据。到目前为止,您提到的只是需要访问数据。除非您需要该功能,否则我不会通过制作对象来夸大您的数据行为。为了避免代码中出现嵌套循环,我会移动逻辑来查询数据到数据库中(假设你有一个);数据库比命令式代码更好,无论是否为OOP。

答案 1 :(得分:0)

考虑将代码分解为:

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";
    }
}

这也使您可以打印任何特定的停靠点或目的地,而无需打印所有目的地,如您给定的嵌套循环,因为打印特定元素的代码与主循环分开。

在这种情况下,虽然可能有点过分,但是更好的方法是提供打印的回调函数,以便数组/对象的遍历始终相同,但是根据您的位置,可以使用多种输出格式。 ;重新打印它(有点像visitor pattern)。