WP Session Manager
[resolved] Can set array like $wp_session['colors'][] = 'daa' (4 posts)

  1. Marko Heijnen
    Posted 3 years ago #

    I just started playing with your code and when I try to add a value like $wp_session['colors'][] = 'daa' or try to unset a value like that. I'm not sure if it should be supported like that.

    When reading the array and use a variable and manipulate that variable there isn't an issue.

    The error is something like: Indirect modification of overloaded property. I found this as a read: http://mwop.net/blog/131-Overloading-arrays-in-PHP-5.2.0.html


  2. Eric Mann
    Plugin Author

    Posted 3 years ago #

    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;
  3. Eric Mann
    Plugin Author

    Posted 3 years ago #

    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!

  4. Eric Mann
    Plugin Author

    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Session Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.