| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * test-pluck.php |
|---|
| 4 | * |
|---|
| 5 | * USAGE: |
|---|
| 6 | * |
|---|
| 7 | * Place into wordpress install folder, then |
|---|
| 8 | * request it via the browser. |
|---|
| 9 | * |
|---|
| 10 | * @see http://core.trac.wordpress.org/ticket/16895 |
|---|
| 11 | */ |
|---|
| 12 | error_reporting(-1); ini_set('display_errors', 1); |
|---|
| 13 | |
|---|
| 14 | require dirname(__FILE__) . '/wp-load.php'; |
|---|
| 15 | |
|---|
| 16 | class NumClass { |
|---|
| 17 | public $num; |
|---|
| 18 | public function __construct() { |
|---|
| 19 | $this->num = 1; |
|---|
| 20 | } |
|---|
| 21 | public function setRef(&$ref) { |
|---|
| 22 | $this->num = &$ref; |
|---|
| 23 | } |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | $value_one = 1; |
|---|
| 27 | $obj_one = new NumClass(); |
|---|
| 28 | $obj_one_ref = new NumClass(); |
|---|
| 29 | $obj_one_ref->setRef($value_one); |
|---|
| 30 | |
|---|
| 31 | $array_one = array('num' => $value_one); |
|---|
| 32 | $array_two = array('num' => 2); |
|---|
| 33 | $array_one_ref = array('num' => &$value_one ); |
|---|
| 34 | |
|---|
| 35 | $list_array = array( |
|---|
| 36 | $array_one, |
|---|
| 37 | $array_two |
|---|
| 38 | ); |
|---|
| 39 | |
|---|
| 40 | $list_array_ref = array( |
|---|
| 41 | &$array_one, |
|---|
| 42 | &$array_one |
|---|
| 43 | ); |
|---|
| 44 | |
|---|
| 45 | $list_array_ref_ref = array( |
|---|
| 46 | &$array_one_ref, |
|---|
| 47 | &$array_one_ref |
|---|
| 48 | ); |
|---|
| 49 | |
|---|
| 50 | $list_obj = array( |
|---|
| 51 | $obj_one, |
|---|
| 52 | $obj_one |
|---|
| 53 | ); |
|---|
| 54 | |
|---|
| 55 | $list_obj_ref = array( |
|---|
| 56 | &$obj_one, |
|---|
| 57 | &$obj_one |
|---|
| 58 | ); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | $list_obj_ref_ref = array( |
|---|
| 62 | &$obj_one_ref, |
|---|
| 63 | &$obj_one_ref |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | $tests = array( |
|---|
| 68 | 'Arrays w/o Refernces' => $list_array, |
|---|
| 69 | 'Arrays with References' => $list_array_ref, |
|---|
| 70 | 'Arrays with References and Referenced Values' => $list_array_ref_ref, |
|---|
| 71 | 'Objects with References and Referenced Values' => $list_obj_ref_ref, |
|---|
| 72 | 'Objects with References' => $list_obj_ref, |
|---|
| 73 | 'Objects' => $list_obj, |
|---|
| 74 | ); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | foreach($tests as $label => $test) { |
|---|
| 78 | echo $label, ":<br>\n"; |
|---|
| 79 | $value_one = 1; |
|---|
| 80 | var_dump($test); |
|---|
| 81 | $nums = wp_list_pluck($test, 'num'); |
|---|
| 82 | $value_one = 11; |
|---|
| 83 | var_dump($nums); |
|---|
| 84 | unset($nums); |
|---|
| 85 | echo '<hr>'; |
|---|
| 86 | } |
|---|