Edward
Edward

Reputation: 9778

Copy/clone parts of an object?

I have an object $stdClassObject that's created by json_decode. It's for train schedules. I need to be able to create a new object which contains just those trains heading east bound. I'm at a loss of how to do this since I'm very new to Objects in PHP. Here is the PHP code I've isolate $trips[$ny_trip]:

// $stdClassObject originally created by json_decode
foreach ($trips as $ny_trip=>$ny_trip_info) {

    if (East_Bound($ny_trip_info->DESTINATION)) {
    // Copy or clone(?) $trips[$ny_trip]) to an object called east_bound
        ...
    }
}

Upvotes: 2

Views: 84

Answers (1)

hek2mgl
hek2mgl

Reputation: 158020

I see two options:

  • Implement the magic method __clone() to be able to customize the process of cloning
  • Add a custom function for this to your class. Like YourClass::customClone($options)

Looks like that the first option suites very well here

Upvotes: 2

Related Questions