|
| 1 | +--TEST-- |
| 2 | +Zend: Test object_init_with_constructor() API for objects without constructors |
| 3 | +--EXTENSIONS-- |
| 4 | +zend_test |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | + |
| 8 | +class TestUserWithoutConstructor { |
| 9 | + public function __destruct() { |
| 10 | + echo 'Destructor for ', __CLASS__, PHP_EOL; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +echo "#### Passing no args ####\n"; |
| 15 | +echo "Userland class:\n"; |
| 16 | +echo "Using new:\n"; |
| 17 | +$o = new TestUserWithoutConstructor(); |
| 18 | +var_dump($o); |
| 19 | +unset($o); |
| 20 | +echo "Using zend_object_init_with_constructor():\n"; |
| 21 | +$o = zend_object_init_with_constructor("TestUserWithoutConstructor"); |
| 22 | +var_dump($o); |
| 23 | +unset($o); |
| 24 | + |
| 25 | +echo "Internal class:\n"; |
| 26 | +echo "Using new:\n"; |
| 27 | +$o = new _ZendTestMagicCall(); |
| 28 | +var_dump($o); |
| 29 | +unset($o); |
| 30 | +echo "Using zend_object_init_with_constructor():\n"; |
| 31 | +$o = zend_object_init_with_constructor("_ZendTestMagicCall"); |
| 32 | +var_dump($o); |
| 33 | +unset($o); |
| 34 | + |
| 35 | +echo "\n#### Passing extra positional args ####\n"; |
| 36 | +echo "Userland class:\n"; |
| 37 | +echo "Using new:\n"; |
| 38 | +$o = new TestUserWithoutConstructor('position_arg'); |
| 39 | +var_dump($o); |
| 40 | +unset($o); |
| 41 | +echo "Using zend_object_init_with_constructor():\n"; |
| 42 | +$o = zend_object_init_with_constructor("TestUserWithoutConstructor", 'position_arg'); |
| 43 | +var_dump($o); |
| 44 | +unset($o); |
| 45 | + |
| 46 | +echo "Internal class:\n"; |
| 47 | +echo "Using new:\n"; |
| 48 | +try { |
| 49 | + $o = new _ZendTestMagicCall('position_arg'); |
| 50 | + var_dump($o); |
| 51 | + unset($o); |
| 52 | +} catch (\Throwable $e) { |
| 53 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 54 | +} |
| 55 | +echo "Using zend_object_init_with_constructor():\n"; |
| 56 | +try { |
| 57 | + $o = zend_object_init_with_constructor("_ZendTestMagicCall", 'position_arg'); |
| 58 | + var_dump($o); |
| 59 | + unset($o); |
| 60 | +} catch (\Throwable $e) { |
| 61 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 62 | +} |
| 63 | + |
| 64 | +echo "\n#### Passing extra named args ####\n"; |
| 65 | +echo "Userland class:\n"; |
| 66 | +echo "Using new:\n"; |
| 67 | +try { |
| 68 | + $o = new TestUserWithoutConstructor(unknown_param: 'named_arg'); |
| 69 | + var_dump($o); |
| 70 | + unset($o); |
| 71 | +} catch (\Throwable $e) { |
| 72 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 73 | +} |
| 74 | +echo "Using zend_object_init_with_constructor():\n"; |
| 75 | +try { |
| 76 | + $o = zend_object_init_with_constructor("TestUserWithoutConstructor", unknown_param: 'named_arg'); |
| 77 | + var_dump($o); |
| 78 | + unset($o); |
| 79 | +} catch (\Throwable $e) { |
| 80 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 81 | +} |
| 82 | + |
| 83 | +echo "Internal class:\n"; |
| 84 | +echo "Using new:\n"; |
| 85 | +try { |
| 86 | + $o = new _ZendTestMagicCall(unknown_param: 'named_arg'); |
| 87 | + var_dump($o); |
| 88 | + unset($o); |
| 89 | +} catch (\Throwable $e) { |
| 90 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 91 | +} |
| 92 | +echo "Using zend_object_init_with_constructor():\n"; |
| 93 | +try { |
| 94 | + $o = zend_object_init_with_constructor("_ZendTestMagicCall", unknown_param: 'named_arg'); |
| 95 | + var_dump($o); |
| 96 | + unset($o); |
| 97 | +} catch (\Throwable $e) { |
| 98 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 99 | +} |
| 100 | + |
| 101 | +?> |
| 102 | +--EXPECT-- |
| 103 | +#### Passing no args #### |
| 104 | +Userland class: |
| 105 | +Using new: |
| 106 | +object(TestUserWithoutConstructor)#1 (0) { |
| 107 | +} |
| 108 | +Destructor for TestUserWithoutConstructor |
| 109 | +Using zend_object_init_with_constructor(): |
| 110 | +object(TestUserWithoutConstructor)#1 (0) { |
| 111 | +} |
| 112 | +Destructor for TestUserWithoutConstructor |
| 113 | +Internal class: |
| 114 | +Using new: |
| 115 | +object(_ZendTestMagicCall)#1 (0) { |
| 116 | +} |
| 117 | +Using zend_object_init_with_constructor(): |
| 118 | +object(_ZendTestMagicCall)#1 (0) { |
| 119 | +} |
| 120 | + |
| 121 | +#### Passing extra positional args #### |
| 122 | +Userland class: |
| 123 | +Using new: |
| 124 | +object(TestUserWithoutConstructor)#1 (0) { |
| 125 | +} |
| 126 | +Destructor for TestUserWithoutConstructor |
| 127 | +Using zend_object_init_with_constructor(): |
| 128 | +object(TestUserWithoutConstructor)#1 (0) { |
| 129 | +} |
| 130 | +Destructor for TestUserWithoutConstructor |
| 131 | +Internal class: |
| 132 | +Using new: |
| 133 | +object(_ZendTestMagicCall)#1 (0) { |
| 134 | +} |
| 135 | +Using zend_object_init_with_constructor(): |
| 136 | +object(_ZendTestMagicCall)#1 (0) { |
| 137 | +} |
| 138 | + |
| 139 | +#### Passing extra named args #### |
| 140 | +Userland class: |
| 141 | +Using new: |
| 142 | +Error: Unknown named parameter $unknown_param |
| 143 | +Using zend_object_init_with_constructor(): |
| 144 | +Error: Unknown named parameter $unknown_param |
| 145 | +Internal class: |
| 146 | +Using new: |
| 147 | +Error: Unknown named parameter $unknown_param |
| 148 | +Using zend_object_init_with_constructor(): |
| 149 | +Error: Unknown named parameter $unknown_param |
0 commit comments