Skip to content
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

Property is suddenly undefined #10477

Closed
greew opened this issue Jan 24, 2024 · 7 comments · Fixed by phpstan/phpstan-src#3097
Closed

Property is suddenly undefined #10477

greew opened this issue Jan 24, 2024 · 7 comments · Fixed by phpstan/phpstan-src#3097
Labels
Milestone

Comments

@greew
Copy link

greew commented Jan 24, 2024

Bug report

I have class A which I can change, and class B which I don't have control over.

The output is this:

 ------ -----------------------------------------------
  Line   phpstan.php
 ------ -----------------------------------------------
  10     Dumped type: string
  12     Dumped type: mixed
  14     Access to an undefined property object::$foo.
 ------ -----------------------------------------------

I don't understand 2 things:

  1. Why the type of $this->foo is changed to mixed
  2. Why then $this->foo is suddenly undefined?

Code snippet that reproduces the problem

https://phpstan.org/r/40d3135d-7d52-4ebe-bb1c-7f23b4ecb4f4

Expected output

 ------ -----------------------------------------------
  Line   phpstan.php
 ------ -----------------------------------------------
  10     Dumped type: string
  12     Dumped type: string
 ------ -----------------------------------------------

Did PHPStan help you today? Did it make you happy in any way?

Still love the tool!!

@RobertMe
Copy link

Why the type of $this->foo is changed to mixed

Opening the Try link it's dumping mixed twice for me.

Why then $this->foo is suddenly undefined?

I presume because the foo(&$var) is by reference. So anything can happen with $var which due to the by reference means anything can happen to $this as well.
See also this version where I added a dumpType($this);: https://phpstan.org/r/d1c0c822-d92e-4af2-9cd7-195d46dbc57c As you can see it dumps mixed, as anything could have happened to it.

@ondrejmirtes
Copy link
Member

Interesting behaviour! Passing $this into a function by reference does not do anything: https://3v4l.org/E5oPQ

But passing a different variable changes it: https://3v4l.org/XMTjK

PHPStan should reflect this.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jan 25, 2024
@RobertMe
Copy link

What's more interesting is that it's any other variable , even a renamed $this: https://3v4l.org/3rASP

The reasoning is most likely backwards compatibility with PHP 4. As in PHP 4 class instances were value types (instead of reference types) so had to be passed by reference so the callee could/would actually modify the state of the object (also in the call site and not just in the called functions scope).

@RobertMe
Copy link

Seems like PHP just ignores the by reference directive when the "source" is $this. Also when assigning $this in a local assignment (and then reassigning the "destination"): https://3v4l.org/J1j0K

@greew
Copy link
Author

greew commented May 13, 2024

Well, if you try and assign something to $this, it fails: https://3v4l.org/O6KAs - so yes, maybe $this ought to have some special meaning regarding references.

@greew
Copy link
Author

greew commented May 13, 2024

It looks like the reference is broken if you try and change the $this value: https://3v4l.org/W17AG

Copy link

github-actions bot commented Jul 1, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants