php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77226 convert object which has invisible properties to array bug
Submitted: 2018-12-02 14:28 UTC Modified: 2018-12-02 14:59 UTC
From: twose at qq dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: master-Git-2018-12-02 (Git) OS: ALL
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: twose at qq dot com
New email:
PHP Version: OS:

 

 [2018-12-02 14:28 UTC] twose at qq dot com
Description:
------------
Amazing bug.
when we convert object which has invisible properties to array, it uses the zend_mangle_property_name result.
like:
array(3) {
  ["0x00Test0x00private"]=>
  int(1)
  ["0x00*0x00protected"]=>
  int(2)
  ["public"]=>
  int(3)
}

I think we can do what the `json_encode` does, skip protected and private members.
I will commit a patch in Github.



Test script:
---------------
<?php
$array = new class
{
    private $private = 1;
    protected $protected = 2;
    public $public = 3;
};
$array = (array)$array;
var_dump($array);
assert(count($array) === 1);
assert(!isset($array['private']));
assert(!isset($array['protected']));
assert($array['public'] === 3);

class Test
{
    private $private = 1;
    protected $protected = 2;
    public $public = 3;
}

$array = (array)new Test;
var_dump($array);
assert(count($array) === 1);
assert(!isset($array['private']));
assert(!isset($array['protected']));
assert($array['public'] === 3);

class TestEx extends Test
{
    private $private_ex = 1;
    protected $protected_ex = 2;
    public $public_ex = 3;
}

$array = (array)new TestEx;
var_dump($array);
assert(count($array) === 2);
assert(!isset($array['private']));
assert(!isset($array['protected']));
assert($array['public'] === 3);
assert(!isset($array['private_ex']));
assert(!isset($array['protected_ex']));
assert($array['public_ex'] === 3);

class TestOver extends Test
{
    private $private = 4;
    protected $protected = 5;
    public $public = 6;
}

$array = (array)new TestOver;
var_dump($array);
assert(count($array) === 1);
assert(!isset($array['private']));
assert(!isset($array['protected']));
assert($array['public'] === 6);

class TestOverOver extends TestOver
{
    public $private = 7;
    public $protected = 8;
    public $public = 9;
    public $private_ex = 10;
    public $protected_ex = 11;
    public $public_ex = 12;
}

$array = (array)new TestOverOver;
var_dump($array);
assert(count($array) === 6);
assert($array['private'] === 7);
assert($array['protected'] === 8);
assert($array['public'] === 9);
assert($array['private_ex'] === 10);
assert($array['protected_ex'] === 11);
assert($array['public_ex'] === 12);

echo "Done\n";


Expected result:
----------------
array(1) {
  ["public"]=>
  int(3)
}
array(1) {
  ["public"]=>
  int(3)
}
array(2) {
  ["public_ex"]=>
  int(3)
  ["public"]=>
  int(3)
}
array(1) {
  ["public"]=>
  int(6)
}
array(6) {
  ["private"]=>
  int(7)
  ["protected"]=>
  int(8)
  ["public"]=>
  int(9)
  ["private_ex"]=>
  int(10)
  ["protected_ex"]=>
  int(11)
  ["public_ex"]=>
  int(12)
}
Done

Actual result:
--------------
because it have 0x00
6172 7261 7928 3329 207b 0a20 205b 2200
636c 6173 7340 616e 6f6e 796d 6f75 7300
2f55 7365 7273 2f74 776f 7365 652f 546f
6173 742f 7377 6f6f 6c65 2d73 7263 2f44
6562 7567 2f61 7272 6179 2e70 6870 3078
3130 6664 3439 3633 3300 7072 6976 6174
6522 5d3d 3e0a 2020 696e 7428 3129 0a20
205b 2200 2a00 7072 6f74 6563 7465 6422
5d3d 3e0a 2020 696e 7428 3229 0a20 205b
2270 7562 6c69 6322 5d3d 3e0a 2020 696e
7428 3329 0a7d 0a0a 5761 726e 696e 673a
2061 7373 6572 7428 293a 2061 7373 6572
7428 636f 756e 7428 2461 7272 6179 2920
3d3d 3d20 3129 2066 6169 6c65 6420 696e
202f 5573 6572 732f 7477 6f73 6565 2f54
6f61 7374 2f73 776f 6f6c 652d 7372 632f
4465 6275 672f 6172 7261 792e 7068 7020
6f6e 206c 696e 6520 3733 0a61 7272 6179
2833 2920 7b0a 2020 5b22 0054 6573 7400
7072 6976 6174 6522 5d3d 3e0a 2020 696e
7428 3129 0a20 205b 2200 2a00 7072 6f74
6563 7465 6422 5d3d 3e0a 2020 696e 7428
3229 0a20 205b 2270 7562 6c69 6322 5d3d
3e0a 2020 696e 7428 3329 0a7d 0a0a 5761
726e 696e 673a 2061 7373 6572 7428 293a
2061 7373 6572 7428 636f 756e 7428 2461
7272 6179 2920 3d3d 3d20 3129 2066 6169
6c65 6420 696e 202f 5573 6572 732f 7477
6f73 6565 2f54 6f61 7374 2f73 776f 6f6c
652d 7372 632f 4465 6275 672f 6172 7261
792e 7068 7020 6f6e 206c 696e 6520 3837
0a61 7272 6179 2836 2920 7b0a 2020 5b22
0054 6573 7445 7800 7072 6976 6174 655f
6578 225d 3d3e 0a20 2069 6e74 2831 290a
2020 5b22 002a 0070 726f 7465 6374 6564
5f65 7822 5d3d 3e0a 2020 696e 7428 3229
0a20 205b 2270 7562 6c69 635f 6578 225d
3d3e 0a20 2069 6e74 2833 290a 2020 5b22
0054 6573 7400 7072 6976 6174 6522 5d3d
3e0a 2020 696e 7428 3129 0a20 205b 2200
2a00 7072 6f74 6563 7465 6422 5d3d 3e0a
2020 696e 7428 3229 0a20 205b 2270 7562
6c69 6322 5d3d 3e0a 2020 696e 7428 3329
0a7d 0a0a 5761 726e 696e 673a 2061 7373
6572 7428 293a 2061 7373 6572 7428 636f
756e 7428 2461 7272 6179 2920 3d3d 3d20
3229 2066 6169 6c65 6420 696e 202f 5573
6572 732f 7477 6f73 6565 2f54 6f61 7374
2f73 776f 6f6c 652d 7372 632f 4465 6275
672f 6172 7261 792e 7068 7020 6f6e 206c
696e 6520 3130 310a 6172 7261 7928 3429
207b 0a20 205b 2200 5465 7374 4f76 6572
0070 7269 7661 7465 225d 3d3e 0a20 2069
6e74 2834 290a 2020 5b22 002a 0070 726f
7465 6374 6564 225d 3d3e 0a20 2069 6e74
2835 290a 2020 5b22 7075 626c 6963 225d
3d3e 0a20 2069 6e74 2836 290a 2020 5b22
0054 6573 7400 7072 6976 6174 6522 5d3d
3e0a 2020 696e 7428 3129 0a7d 0a0a 5761
726e 696e 673a 2061 7373 6572 7428 293a
2061 7373 6572 7428 636f 756e 7428 2461
7272 6179 2920 3d3d 3d20 3129 2066 6169
6c65 6420 696e 202f 5573 6572 732f 7477
6f73 6565 2f54 6f61 7374 2f73 776f 6f6c
652d 7372 632f 4465 6275 672f 6172 7261
792e 7068 7020 6f6e 206c 696e 6520 3131
380a 6172 7261 7928 3829 207b 0a20 205b
2270 7269 7661 7465 225d 3d3e 0a20 2069
6e74 2837 290a 2020 5b22 7072 6f74 6563
7465 6422 5d3d 3e0a 2020 696e 7428 3829
0a20 205b 2270 7562 6c69 6322 5d3d 3e0a
2020 696e 7428 3929 0a20 205b 2270 7269
7661 7465 5f65 7822 5d3d 3e0a 2020 696e
7428 3130 290a 2020 5b22 7072 6f74 6563
7465 645f 6578 225d 3d3e 0a20 2069 6e74
2831 3129 0a20 205b 2270 7562 6c69 635f
6578 225d 3d3e 0a20 2069 6e74 2831 3229
0a20 205b 2200 5465 7374 4f76 6572 0070
7269 7661 7465 225d 3d3e 0a20 2069 6e74
2834 290a 2020 5b22 0054 6573 7400 7072
6976 6174 6522 5d3d 3e0a 2020 696e 7428
3129 0a7d 0a0a 5761 726e 696e 673a 2061
7373 6572 7428 293a 2061 7373 6572 7428
636f 756e 7428 2461 7272 6179 2920 3d3d
3d20 3629 2066 6169 6c65 6420 696e 202f
5573 6572 732f 7477 6f73 6565 2f54 6f61
7374 2f73 776f 6f6c 652d 7372 632f 4465
6275 672f 6172 7261 792e 7068 7020 6f6e
206c 696e 6520 3133 350a 446f 6e65 0a

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-02 14:54 UTC] twose at qq dot com
-Status: Open +Status: Closed
 [2018-12-02 14:54 UTC] twose at qq dot com
it's not a bug, that's it.
 [2018-12-02 14:59 UTC] nikic@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC