Indirect modification of overloaded property WP_Post::$ancestors has no effect

As I understand, the property WP_Post::$ancestors is created dynamically. So, if you try to access to it when it doesn’t exist, PHP calls the magic method __get on that object and try to get the value of that property.

Because the property is dinamically created, you can not modify it like you do in this line (end() function modifies the internal pointer position of the array):

end($post->ancestors);

This message from PHP can be avoided if the __get method returns by reference, but you can not modify the core code (and I’m not sure if returning by reference in WP_Post::__get is appropiated anyway), so you should try to modify your own variable with post ancestors:

        if(!$post->post_parent){
            $children = wp_list_pages(array(
                'sort_column' => 'menu_order',
                'title_li'    => '',
                'echo'        => 1,
                'walker' => new Sidebar_Custom_Walker()
            ));
        }else{

            $ancestors = $post->ancestors;
            // Using get_post_ancestors() should work as well
            // $ancestors = get_post_ancestors();

            if( !empty( $ancestors ) )
            {
                $ancestors = end($ancestors);

                $children = wp_list_pages(array(
                    'sort_column' => 'menu_order',
                    'title_li'    => '',
                    'echo'        => 1,
                    'child_of'=>$ancestors,
                    'walker' => new Sidebar_Custom_Walker()
                ));
            }
        }