Skip to content

Implement typed properties #3734

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
wants to merge 56 commits into from
Closed

Implement typed properties #3734

wants to merge 56 commits into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Jan 7, 2019

RFC: https://wiki.php.net/rfc/typed_properties_v2

This is a squashed version of #3313 to make final review easier. The previous thread has become unwieldy.

@nikic nikic added the RFC label Jan 7, 2019
Copy link
Contributor

@carusogabriel carusogabriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic From the tests part, just a couple of tests, for example, Zend/tests/type_declarations/typed_properties_019.phpt, that have a fell extra lines at the end of the lines. I'll run this implementation in some OSS projects, see if I can found some missing tests 👍

@nikic
Copy link
Member Author

nikic commented Jan 7, 2019

@carusogabriel I've added some basic property type support to https://github.com/nikic/TypeUtil for this purpose. For now I've only tested that PHP-Parser works after automatically annotating it with property types (needs some manual fixup to fix inaccurate @var annotations and different initialization semantics of typed properties). Would be great to do these kinds of tests on more projects.

@nikic nikic mentioned this pull request Jan 7, 2019
@nikic

This comment has been minimized.

@nikic

This comment has been minimized.

@bwoebi

This comment has been minimized.

@nikic

This comment has been minimized.

nikic and others added 22 commits January 10, 2019 12:43
RFC: https://wiki.php.net/rfc/typed_properties_v2

Co-authored-by: Bob Weinand <bobwei9@hotmail.com>
Co-authored-by: Joe Watkins <krakjoe@php.net>
Co-authored-by: Dmitry Stogov <dmitry@zend.com>
Add two more macros ZEND_TRY_ASSIGN_VALUE and ZEND_TRY_ASSIGN_COPY
to assign zvals and replace all remaining uses of the internal
API.

Switch the API to not use a temporary zval anymore, as all users
of the API take care to do this themselves.
I found the 1+count here much more confusing than helpful.
And add tests for typed properties + references + undefined classes.
I think the behavior here is not correct and we should treat this the
same way we do elsewhere (if the class can't be loaded, conclude that
it can't be an instance of the class, but don't issue an error).

Going back to this behavior for now to avoid an assertion failure...
This is probably going to need changes in a few more places that
currently implicitly assume only CE types can occur.
If the value_type is not known at compile-time, don't perform checks
for whether the copy is needed or not, always do it. Also make sure
we free the copy in the error case...
If we're in a class that has type hints, actually seeing a type is
not unexpected.
This makes it consistent with the function for ref types and avoids
having to handle these differently.
And remove some code handling future possibilities where a property
could be both int and double. I don't like having untestable code
around. Let's deal with this fully once we have union types.
And remove some redundant checks.
nikic added 8 commits January 10, 2019 12:43
Made a mistake here when refactoring the code, causing a failure
in serialization test.
Based on the descision that we do not want to enforce __get() return
types for inaccessible propreties.
It's not relevant in any other case and might be wrong wrt cache
state I think.
We have the proprerty offset here and know it's valid, so we can
use our property helper table to fetch it, rather than doing a
full property info lookup.
By switching to the slot API, which doesn't need unmangled keys
and is faster anyway...

Also moving those APIs to a place that has a complete prop_info
def.
I originall thought this wasn't possible, but now that it turned out
that object_handlers also always has slots available, we can do this.
Otherwise we can probably run into the same issues here as we could
with destruction.
nikic added 15 commits January 10, 2019 13:14
Used in conjunction with try_array_init, can't deref beforehand.
I'm explicitly passing the object to process_nested_data now.
When I tried to use Z_OBJ_P(rval) things started failing in odd
ways and I have no idea why. The variable doesn't seem to be
overwritten...
It's rather unlikely that these will be available as CEs at this
point, we need to look them up.
I don't think it can happen right now, but will once there is full
preloading support for this.
php-pulls pushed a commit that referenced this pull request Jan 11, 2019
RFC: https://wiki.php.net/rfc/typed_properties_v2

This is a squash of PR #3734, which is a squash of PR #3313.

Co-authored-by: Bob Weinand <bobwei9@hotmail.com>
Co-authored-by: Joe Watkins <krakjoe@php.net>
Co-authored-by: Dmitry Stogov <dmitry@zend.com>
@nikic
Copy link
Member Author

nikic commented Jan 11, 2019

Merged via e219ec1.

@moliata
Copy link
Contributor

moliata commented Mar 24, 2020

@nikic, is there any chance for typed callable properties in PHP 8? Personally, this is the only thing holding me back from fully loving PHP. Even though the context problems outlined in the consistent callables RFC are fair and square, language consistency is also important and much appreciated. Typed parameters and return types basically ignore the fact whether the callable is actually callable from this scope/context. This isn't the case with properties. Here are my reasons for supporting typed callable properties:

  • Language consistency. Given that PHP is becoming a much more consistent and powerful language with every release, adding support for typed callable properties would improve the overall consistency as both typed callable parameters and return types are supported. Futhermore, thanks to the upcoming union types, you would be able to even completely type hint your entire project. Though that isn't possible yet due to the lack of support for callable properties.
  • Strict typing. Strictly limiting a property to only callables helps to avoid bugs, makes debugging process easier and in some use cases (such as frameworks) ensures that the application is not going to have side effects and will be reliable.
  • Closure::fromCallable() adds additional overhead. Assuming that thousands of callables need to be converted (which is often the case in frameworks when registering hundreds of routes to the router on large web applications).

Best regards,
Ben.

@nikic
Copy link
Member Author

nikic commented Mar 24, 2020

@moliata I doubt we will ever support callable for properties. The general consensus is that we should make it easier to create Closures from generic callables instead. (That is, add first-class syntax to reference a function/method, instead of using magic strings/arrays).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants