0

I wrote some program to check the value in an array.

var_dump($profileuser);//NULL

$profileuser = get_user_to_edit($user_id);//fetch the value of $profileuser

var_dump($profileuser);//does not print the value of $profileuser->user_url 
//nor by print_r($profileuser)

if(isset($profileuser->user_url))
    echo $profileuser->user_url;//printed!!!!How is it possible??

Could somebody can explain how this happened?

background:

I modified the kernel of wordpress.

This happened when I modified the file of wp-admin/user-edit.php.

Felipe
  • 11,557
  • 7
  • 56
  • 103
Ray
  • 568
  • 6
  • 20
  • Doesn't sound plausible, are you sure there are no typos in names? – Hanky Panky May 03 '14 at 15:07
  • Add a call to `exit;` after `var_dump($profileuser);` and tell us what you see. – Felipe May 03 '14 at 15:35
  • @FelipeAlmeida I had done what you want me to do. There are too much words that I can not print all of them. However, there is no $profileuser->user_url inside. Only shown the value of $profileuser->data-user_url; but this value is different from $profileuser->user_url. – Ray May 03 '14 at 15:40
  • @Hanky웃Panky Pardon that I don't know what does typos means. Can you explain more specific? – Ray May 03 '14 at 15:42
  • 1
    @Ray [typo on Wikipedia](http://en.wikipedia.org/wiki/Typographical_error). Typos are several typographical errors (spelling errors). – h2ooooooo May 03 '14 at 16:29

3 Answers3

2

You say it's an array, but you're accessing it as an object ($obj->foo rather than $arr['foo']), so it's most likely an object (actually it is - get_user_to_edit returns a WP_User). It could easily contain the magic __get and __isset methods that would lead to this behaviour:

<?php

class User {
    public $id = 'foo';

    public function __get($var) {
        if ($var === 'user_url') {
            return 'I am right here!';
        }
    }

    public function __isset($var) {
        if ($var === 'user_url') {
            return true;
        }

        return false;
    }
}

$user = new User();

print_r($user);
/*
    User Object
    (
        [id] => foo
    )
*/

var_dump( isset($user->user_url) ); // bool(true)
var_dump( $user->user_url ); // string(16) "I am right here!"

DEMO

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • 2
    This is what's happening. `get_user_to_edit` returns a [WP_User](http://codex.wordpress.org/Class_Reference/WP_User) object, and that [object](https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/capabilities.php#L399) does have [magic methods](https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/capabilities.php#L617). – vvanasten May 03 '14 at 16:17
  • Thanks a lot. I finally figure out!! – Ray May 03 '14 at 16:53
1

One possibility is that $profileuser is an Object that behaves as an array and not an array itself.

This is possible with interface ArrayAccess. In this case, isset() would return true and you might not see it when you do var_dump($profileuser);.

When you want an object to behave like an array, you need to implement some methods which tell your object what to do when people use it as if it were an array. In that case, you could even create an Object that, when accessed as an array, fetches some webservice and return the value. That may be why you are not seeing those values when you var_dump your variable.

Felipe
  • 11,557
  • 7
  • 56
  • 103
  • Thank you, but @h2ooooooo give me program as an example. Therefore, I gave him the best answer. Thank you also!! – Ray May 03 '14 at 16:56
0

I thinks it's not possible, I've created test code and var_dump behaves correctly. Are you 100% sure you don't have any typo in your code? I remind variables in PHP are case sensitive

<?php


$profileuser = null;


class User
{
  public $user_url;

}
function get_user_to_edit($id) {
$x = new User();  
  $x->user_url = 'vcv';
  return $x;

}

var_dump($profileuser);//NULL
$user_id = 10;
$profileuser = get_user_to_edit($user_id);//fetch the value of $profileuser

var_dump($profileuser);//does not print the value of $profileuser->user_url 
//nor by print_r($profileuser)

if(isset($profileuser->user_url))
  echo $profileuser->user_url;//printed!!!!How does it possible??

Result is:

null

object(User)[1]
  public 'user_url' => string 'vcv' (length=3)

vcv
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • I used the wordpress version 3.9 and the code was added in the file of wp-admin/user-edit.php line 185. After the var_dump called, there are almost 30 variables shown but not $profileuser->user_url. – Ray May 03 '14 at 15:50