-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/pgsql: clean up pg_fetch_object() constructor handling. #21884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+96
−14
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --TEST-- | ||
| pg_fetch_object() constructor handling: ctor_params validation, throwing constructor, property visibility | ||
| --EXTENSIONS-- | ||
| pgsql | ||
| --SKIPIF-- | ||
| <?php include("inc/skipif.inc"); ?> | ||
| --FILE-- | ||
| <?php | ||
| include 'inc/config.inc'; | ||
|
|
||
| class NoCtor {} | ||
|
|
||
| class ThrowingCtor { | ||
| public function __construct() { | ||
| throw new RuntimeException('boom'); | ||
| } | ||
| public function __destruct() { | ||
| echo "ThrowingCtor::__destruct called (BUG)\n"; | ||
| } | ||
| } | ||
|
|
||
| class SeesProps { | ||
| public function __construct() { | ||
| echo "ctor sees: num={$this->num}, str={$this->str}\n"; | ||
| } | ||
| public function __destruct() { | ||
| echo "SeesProps::__destruct called\n"; | ||
| } | ||
| } | ||
|
|
||
| $table_name = "pg_fetch_object_ctor_paths"; | ||
| $db = pg_connect($conn_str); | ||
| pg_query($db, "CREATE TABLE {$table_name} (num int, str text)"); | ||
| pg_query($db, "INSERT INTO {$table_name} VALUES(1, 'hello')"); | ||
|
|
||
| $sql = "SELECT * FROM {$table_name} WHERE num = 1"; | ||
|
|
||
| // 1) ctor_params on a class with no constructor must throw ValueError | ||
| try { | ||
| pg_fetch_object(pg_query($db, $sql), null, 'NoCtor', [1, 2]); | ||
| } catch (ValueError $e) { | ||
| echo $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| // 2) Constructor that throws: __destruct must NOT run on the partially constructed object | ||
| try { | ||
| pg_fetch_object(pg_query($db, $sql), null, 'ThrowingCtor'); | ||
| } catch (RuntimeException $e) { | ||
| echo "caught: ", $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| // 3) Constructor sees row properties already merged onto $this | ||
| $obj = pg_fetch_object(pg_query($db, $sql), null, 'SeesProps'); | ||
| unset($obj); | ||
|
|
||
| echo "Ok\n"; | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| include('inc/config.inc'); | ||
| $db = pg_connect($conn_str); | ||
| pg_query($db, "DROP TABLE IF EXISTS pg_fetch_object_ctor_paths"); | ||
| ?> | ||
| --EXPECT-- | ||
| pg_fetch_object(): Argument #4 ($constructor_args) must be empty when the specified class (NoCtor) does not have a constructor | ||
| caught: boom | ||
| ctor sees: num=1, str=hello | ||
| SeesProps::__destruct called | ||
| Ok |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use
object_init_with_constructor()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion I ll try it later ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion again. however
object_init_with_constructordoesn't quite fit here:pg_fetch_objecthas to merge the fetched row onto $this between init and the constructorcall so user constructors can read $this->num etc., and the helper does both in one shot (its internal
_object_and_properties_initwould also discard any pre-mergedproperties).
The manual code mirrors the helper's structure (
object init,get_constructorunderEG(fake_scope),zend_object_store_ctor_failedon throw); the only divergence is theproperty merge slotted between the two phases. Happy to revisit if I'm missing an angle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blerg, it does the weird PDO behaviour... I guess ideally it should pass all the row elements as arguments to the constructor, but if we do this we should create new APIs for all the DB extensions.