Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#16 Today 07:33:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,506
Website GitHub

Re: Callback for saving own new fields

Ah yes, that’s true about the pre/post in this case. Sorry, I didn’t take account of the actual callback name article_submit, d’oh.

The ampersand treats the passed value by reference. So, rather than making a local copy of the variable and working on that, which gets destroyed when the function exits (barring any return), it operates on the external variable directly.

Unless a callback explicitly assigns whatever you return to its own (or another) variable, returning something from a callback function will do nothing. In those cases, an ampersand usually works better.

So, you can usually return something from these callbacks:

$some_variable = callback_event($evt, $stp, $data);

But you would use an ampersand on your received variable in your function signature if the callback was one of these:

callback_event($evt, $stp, $data);
callback_event_ref($evt, $stp, $data);

Annoyingly there’s no way to know which is which, without looking at the code, so one of the things I’d like to do is adopt a solid naming convention to make it obvious from the callback event/step/pre which type it is.

Generally though, if it’s a callback_event_ref then you need to use an ampersand. As you found, your variable name doesn’t need to match the external one: that’s your reference copy, like a pointer to the external variable you’re manipulating.

Hope that helps.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB