WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Marko Heijnen
    Member
    Posted 1 year 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

    http://wordpress.org/extend/plugins/wp-session-manager/

  2. Eric Mann
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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

About this Topic

Tags

No tags yet.