xref: /PHP-8.4/ext/standard/tests/array/bug77135.phpt (revision 83cdb89f)
1--TEST--
2Test extract() with $this
3--FILE--
4<?php
5
6class Extract
7{
8    public function run(): void
9    {
10        $options = [
11            'EXTR_OVERWRITE' => EXTR_OVERWRITE,
12            'EXTR_SKIP' => EXTR_SKIP,
13            'EXTR_PREFIX_SAME' => EXTR_PREFIX_SAME,
14            'EXTR_PREFIX_ALL' => EXTR_PREFIX_ALL,
15            'EXTR_PREFIX_INVALID' => EXTR_PREFIX_INVALID,
16            'EXTR_IF_EXISTS' => EXTR_IF_EXISTS,
17            'EXTR_PREFIX_IF_EXISTS' => EXTR_PREFIX_IF_EXISTS,
18        ];
19
20        foreach ($options as $name => $flags) {
21            echo "{$name}\n";
22
23            $this->handle($name, $flags);
24            $this->handle("{$name}_REFS", $flags | EXTR_REFS);
25            echo "\n";
26        }
27    }
28
29    private function handle(string $name, int $flags): void
30    {
31        $array = ["this" => "value"];
32
33        try {
34            $result = extract($array, $flags, "x");
35            echo "  extract() = {$result}\n";
36
37            echo "  \$this = " . get_class($this) . "\n";
38            echo "  \$v_this = " . (isset($x_this) ? $x_this : "NULL") . "\n";
39        } catch (\Throwable $e) {
40            echo "  Exception: " . $e->getMessage() . "\n";
41        }
42    }
43}
44
45(new Extract)->run();
46
47?>
48--EXPECT--
49EXTR_OVERWRITE
50  Exception: Cannot re-assign $this
51  Exception: Cannot re-assign $this
52
53EXTR_SKIP
54  extract() = 0
55  $this = Extract
56  $v_this = NULL
57  extract() = 0
58  $this = Extract
59  $v_this = NULL
60
61EXTR_PREFIX_SAME
62  extract() = 1
63  $this = Extract
64  $v_this = value
65  extract() = 1
66  $this = Extract
67  $v_this = value
68
69EXTR_PREFIX_ALL
70  extract() = 1
71  $this = Extract
72  $v_this = value
73  extract() = 1
74  $this = Extract
75  $v_this = value
76
77EXTR_PREFIX_INVALID
78  extract() = 1
79  $this = Extract
80  $v_this = value
81  extract() = 1
82  $this = Extract
83  $v_this = value
84
85EXTR_IF_EXISTS
86  extract() = 0
87  $this = Extract
88  $v_this = NULL
89  extract() = 0
90  $this = Extract
91  $v_this = NULL
92
93EXTR_PREFIX_IF_EXISTS
94  extract() = 0
95  $this = Extract
96  $v_this = NULL
97  extract() = 0
98  $this = Extract
99  $v_this = NULL
100