Repository navigation
settype(): document that it always returns true and can throw TypeError - #5916
Merged
lacatoire merged 2 commits intoOct 7, 2026
Merged
Conversation
The return values section used return.success, which says the function
returns false on failure. It cannot return false. I tried an invalid type
name, an array, an object and a resource, and a reference to a typed
property, and it either returned true or threw.
Switched to return.true.always, the entity already used by openlog(),
closelog() and syslog() for the same situation.
The errors section only documented the ValueError for an invalid type
name. A TypeError is also thrown when var is a reference to a typed
property and the converted value cannot be assigned back to the declared
type. Tested on PHP 8.4.26:
declare(strict_types=1);
class Foo { public int $value = 123; }
$foo = new Foo;
$ref = &$foo->value;
settype($ref, "string");
// TypeError: Cannot assign string to reference held by property
// Foo::$value of type int
I wrote it as "cannot be assigned under the active typing mode" instead
of listing the cases, because widening still works in strict mode. With
strict_types a float property set to "integer" returns true and ends up
as float(1), while an int property set to "float" throws.
Added a note for the coercive case, where the converted value is assigned
back and gets coerced to the declared type, so the property keeps its
type and the call still returns true. That is the behaviour reported in
the issue that looked like a bug.
Fixes php#2677
lacatoire
reviewed
Oct 6, 2026
Comment on lines
+167
to
+169
| converted value is coerced back to the declared type of the property. The | ||
| type of the property therefore does not change, and | ||
| <function>settype</function> still returns &true;. |
Member
There was a problem hiding this comment.
Suggested change
| converted value is coerced back to the declared type of the property. The | |
| type of the property therefore does not change, and | |
| <function>settype</function> still returns &true;. | |
| converted value is coerced back to the declared type of the property when | |
| possible, so the type of the property does not change and | |
| <function>settype</function> returns &true;. Otherwise a | |
| <exceptionname>TypeError</exceptionname> is thrown. |
Member
|
Thanks @angeliszotis a small nits, and ci ones but LGTM |
Apply @lacatoire's wording suggestion for the note and switch the three inline-only para blocks to simpara, which the DocBook style check flagged.
Contributor
Author
|
Thanks @lacatoire! Applied your wording for the note and changed the three inline-only |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2677
I checked both points from the report on PHP 8.4.26.
It cannot return false
The return values section used
&return.success;, which renders as "Returnstrueon success orfalseon failure". I tried to make it returnfalse:Every path either returns
trueor throws. Switched to&return.true.always;, which is already used byopenlog(),closelog()andsyslog()for the same situation.TypeError was not documented
The errors section only had the
ValueErrorfor an invalid type name. There is also aTypeError:I wrote it as "cannot be assigned to the declared type under the active typing mode" rather than listing the cases, because widening still works with strict typing:
The case from the report
The original report was about this looking like a bug:
With coercive typing the converted value is assigned back and coerced to the declared type, so the property keeps its type and the call still returns
true. I added a note for that, since the page gave no hint why the conversion appears to do nothing.Changes
returnvalues:&return.success;to&return.true.always;errors: added theTypeErrorparagraphnotes: added the note about assigning the converted value backBoth
linkendtargets I used,language.oop5.propertiesandlanguage.types.declarations.strict, already exist and are used by other pages.