0

I know PHP, but not much with OO in PHP. Working in Joomla, I can see the benefits of using OO and MVC and like the way it appears. But I often run across situations where I don't know if this is a good candiate for OO or not. Here is an example of a train schedule as a JSON string output as print_r.

In order to extract the various train stops from the schedule without using OO in PHP, I end up with nested 'foreach' to get at the train station's departure and arrival times.

Might this be better accomplished treating it as an Object in an environment OO? Would it make it easier to get at the data without all the nested 'foreach' statements to make a more elegant programming environment? Or switching to OO doesn't simplify this task and readable of the PHP code any further?

If you feel OO is the way to go, if you could give an example of how the PHP would look using the example below to improve the environment that would be most helpful. Thanks!

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.

I'm trying to avoid or find a better method so the PHP code doesn't look something like this:

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

Something more direct, perhaps like this: $arrival_time = $train_destination($station->$last_stop);

So maybe functions as was suggested is the better way approach this.

4

2 回答 2

1

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

于 2013-06-13T16:23:16.540 回答
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";
    }
}

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

更好的方法是为打印提供回调函数,尽管在这种情况下可能有点过头了,这样您的数组/对象的遍历总是相同的,但是根据您打印的位置,可能有许多输出格式(有点像访客模式)。

于 2013-06-13T16:47:03.310 回答