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

Variable can be final #363

Closed
rentalhost opened this issue Jun 22, 2017 · 9 comments
Closed

Variable can be final #363

rentalhost opened this issue Jun 22, 2017 · 9 comments
Assignees
Milestone

Comments

@rentalhost
Copy link
Collaborator

It is a big confuse, once that PHP don't have support to final modifier, but we can implement that by creating some king of "variable annotation". Then I think about prefix a variable with $f to identify that it is final and should not be overrided.

// Allowed:
$number = 123;
$number = 456;

// Warned: $number could be final -> $fNumber
$number = 123;
echo $number;

// Not allowed: final variable $fNumber was overrided.
$fNumber = 123;
$fNumber = 456; // or
$fNumber.= 456;

It should works like we did with parameters, by prefixing it by $pName.

@rentalhost rentalhost added this to the 2.3.+ milestone Jun 22, 2017
@kalessil
Copy link
Owner

I think we shouldn't go into meta-programming and wait for the corresponding RFC instead.

@rentalhost
Copy link
Collaborator Author

I don't found any RFC about final variables.
Where did you found it?

@Fleshgrinder
Copy link

Fleshgrinder commented Jun 27, 2017

Introduction of Hungarian notation is not a good idea. I also think that there is no RFC as it would be very hard to implement this in PHP due to the way arrays are implemented, this also stopped the work on the immutable data class RFC. That being said, an annotation could be used:

/**
 * @final
 * @var int
 */
$num = 42;

@rentalhost
Copy link
Collaborator Author

I don't like annotations like that (phpdoc). Unfortunatelly PHP internals doesn't accepts the @Annotation outside a phpdoc. It should resolve the problem for IDEs, like:

@final $num = 42;

I really don't understand. I guess that is a conflict with the silence operator.

The reason for the Hungarian notation is to avoid the phpdoc. But it could be done together, depending of preference of developer.

@kalessil
Copy link
Owner

There were no RFC yet for final variables, but I think we are going to see such RFC in near future =)

@Fleshgrinder
Copy link

Annotations where discussed ad infinitum but no conclusion could be found, too many opinions and hard to implement.

Are you creating one @kalessil?

@davidmpaz
Copy link
Contributor

For the time being, I give +1 to annotations. I also think Hungarian notation will make no good. PHP has more type safety now. As @kalessil said RFC for this, as well as for real Polymorphismus or others related to typing should not be far away.

@Fleshgrinder with:

this also stopped the work on the immutable data class RFC

did you mean this one? https://wiki.php.net/rfc/immutability, if not, can you share the link?

With annotations, something like:

/** @final */
$num = 42

Adds more code that sometimes just look ugly to see even in a local variable usage; but compromise should be made if is desired such checking in code.

Regarding what annotation to use I really don't have any idea what to use, @final looks as good as any other to me.

@Fleshgrinder
Copy link

@davidmpaz that's exactly the one I referred to, yes.

@kalessil
Copy link
Owner

@Fleshgrinder: nope =)

@kalessil kalessil modified the milestones: Backlog, C-2.3.18 Mar 6, 2018
@kalessil kalessil self-assigned this Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants