WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Can set array like $wp_session['colors'][] = 'daa'

[Resolved] Can set array like $wp_session['colors'][] = 'daa'

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Eric Mann
    Member

    @ericmann

    Interesting …

    The issue is actually in the underlying structure of PHP. I’m implementing ArrayAccess for WP_Session which is what gives you the array-like structure of the parent container. Unfortunately, ArrayAccess doesn’t play well with multidimensional arrays.

    For whatever reason, the original PHP authors chose to make ArrayAccess return properties in read mode (not by reference). So when you call $wp_session['colors'] it returns a read-only version of the array. Adding to the array will have no effect.

    A workaround, until I figure out a more permanent solution, would be to:

    $wp_session = WP_Session::get_instance();
    
    $colors = array();
    $colors[] = 'daa';
    $wp_session['colors'] = $colors;
    Plugin Author Eric Mann
    Member

    @ericmann

    Actually, I did find a way to enable this. It involves extending things a bit, but it’s possible.

    Basically, I build a class called RecursiveArrayAccess that implements ArrayAccess. It allows for iterative, multidimensional arrays (and I found it on StackOverflow (http://stackoverflow.com/a/13480254/326224). WP_Session will then extend this class.

    I’m still fleshing things out, but the following is proven to work:

    $wp_session = WP_Session::get_instance();
    $wp_session['colors'] = array();
    $wp_session['colors'][] = 'dda';

    Both sets and unsets seem to work with this pattern, so we should be good to go!

    Plugin Author Eric Mann
    Member

    @ericmann

    Give version 1.1 a try. It implements this new pattern and should fix your problem.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Can set array like $wp_session['colors'][] = 'daa'’ is closed to new replies.