latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

what is eval('foreach …') for?

Home Forums Community Forum what is eval('foreach …') for?

Tagged: 

This topic contains 3 replies, has 3 voices. Last updated by  Jason (Lead Developer) 4 years, 2 months ago.

Topic Author Topic
Posted: Wednesday Sep 26th, 2012 at 8:16 am #26554

All over s2member code I find blocks like this:

eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); //why is eval() necessary?
do_action ("SOME_FUNCTION_NAME", get_defined_vars ());
unset ($__refs, $__v); /* Unset defined __refs, __v. */

What is this code doing and why does it need to be done? I ask this not just out of curiosity, but because I have over-ridden the paypal-form-in.inc.php file for formatting reasons and I dislike maintaining code that I don’t understand.

Thanks.

List Of Topic Replies

Viewing 3 replies - 1 through 3 (of 3 total)
Author Replies
Author Replies
Posted: Thursday Sep 27th, 2012 at 4:58 am #26657
Raam Dev
Username: Raam
Staff Member

The eval() function executes code (see PHP Doc), but I’m not sure why it’s used there. I will send the lead developer a message and see if he can explain. I know that he’s quite busy working on the next major release of s2Member, but he always gets to all his emails. Thank you for your patience.

Posted: Thursday Sep 27th, 2012 at 7:09 am #26673

Hi Raam,

I know what `eval()` does. I just don’t understand why it has been used to wrap a everyday loop with. Thanks for forwarding this on to Jason.

Cheers,
Jen

Posted: Tuesday Oct 16th, 2012 at 8:38 pm #28614
Staff Member

Thanks for the heads up on this thread.

Regarding code snippets like the one you see below, throughout many areas of s2Member’s source code.

eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
do_action ("ws_plugin__s2member_during_custom_registration_fields_4bp_before", get_defined_vars ());
unset ($__refs, $__v); /* Unset defined __refs, __v. */

The first line of code where eval is used, is simply to compress all of this repetitious code into a single line that can pass IDE auto-formatting. The purpose of this repetitious code is as follows.

The first line collects all defined variables in the current scope, and creates PHP “references” to each of those variables, forcing the future call to get_defined_vars() in the next line, to include $__refs, which is a temporary array now containing PHP references to the current variables in scope.

In this way, it makes it possible for site owners/developers to hook into s2Member’s routines, and actually change the value of certain variables inline, by reference. This increases flexibility for developers that wish to customize s2Member-driven routines in more advanced ways.

Viewing 3 replies - 1 through 3 (of 3 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.