PHP and Drupal riddle: what’s wrong here?

What is wrong with this Drupal 7 style function below? Sure, style-wise one may be better off with entity wrappers, but assume we don’t like them or something….

Asked differently, what would be the error or warning printed once you run this function?

Add space here so you’ll need to scroll for the answer…

The problem is that PHP’s clone is shallow; it copies the properties, but doesn’t go deeper than that. So the line immediately after the ‘clear dates and set’ comment will clear both $cnode and $node dates. As a result, all the cloned nodes (as well as the original) will have empty dates, and the first error will read ‘Notice: Undefined offset: 0 in badfunction_extract()’.

Now, how do you fix this function with one line of code?

Once you unset the property, it no longer points to the original node, and you can now safely copy and set the dates.

Like what you read? Give bluetegu a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.