简体   繁体   中英

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

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.

OOP is great for tying data with behaviors. So far, all you've mentioned is needing to access data. I wouldn't inflate your data with behaviors by making objects unless you need that functionality. To avoid nested loops in your code, I'd move logic for querying your data into your database (assuming you have one); databases are better at that sort of thing than imperative code, whether it's OOP or not.

Consider factoring your code like this:

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

This also gives you the possibility to print any specific stop or destination without having to print all destinations like your given nested loops since the code for printing a specific element is separated from the main loop.

Something even better, albeit maybe overkill in this situation, would be to provide a callback function for the printing so that the traversal of your arrays/objects is always the same, but many output formats are possible depending on where you're printing it (a bit like the visitor pattern ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM