News for hackers. Stuff that matters.
Re:PHP 8.4 by eurleif on Thursday November 21, 2024 @03:36PM (#42208369)
I was curious about why setting `$this->countryCode` inside the setter for `countryCode` didn't result in infinite recursion. Turns out this is spelled out in the RFC, but not in the docs:

   When a hook is called, inside that hook $this->[propertyName] will refer to the “unfiltered” value of the property, called the “backing value.” When accessed from anywhere else, $this->[propertyName] calls will go through the relevant hook. This is true for all hooks on the same property. This includes, for example, writing to a property from the get hook; that will write to the backing value, bypassing the set hook.
   
   A normal property has a stored “backing value” that is part of the object, and part of the memory layout of the class. However, if a property has at least one hook, and none of them make use of $this->[propertyName], then no backing value will be created and there will be no data stored in the object automatically (just as if there were no property, just methods). Such properties are known as “virtual properties,” as they have no materialized stored value.
   
   Be aware, the detection logic works on $this->[propertyName] directly at compile time, not on dynamic forms of it like $prop = 'beep'; $this->$prop. That will not trigger a backing value.
Feels like too much magic to me that a property access can mean different things depending on context, but I'm not a PHP user, so I don't get a vote.
Re:PHP 8.4 by tail_exchange on Thursday November 21, 2024 @04:16PM (#42208755)
I'm also not a php developer, but I agree. This seems like a huge footgun.

I've never been a fan of this kind of magic, and I wonder how other languages deal with this case.

Re:PHP 8.4 by crimsonnoodle58 on Thursday November 21, 2024 @04:34PM (#42208906)
Well in python I assume it would be equivalent of accessing:

  self.__dict__['property_name']
And if those PHP rules were in python you could just write:

  @property
  def property_name(self):
      return self.property_name
In actual python though, that would infinitely recurse.
Re:PHP 8.4 by arkh on Friday November 22, 2024 @03:44AM (#42212165)
Or if they could follow Godot syntax: https://docs.godotengine.org/en/stable/tutorials/scripting/g...

var milliseconds: int = 0

var seconds: int:

get:

  return milliseconds / 1000

 set(value):

  milliseconds = value * 1000
Re:PHP 8.4 by kijin on Thursday November 21, 2024 @10:18PM (#42210827)
It might be a footgun, but it will be extremely obvious when it is triggered.
Re:PHP 8.4 by Liquidor on Friday November 22, 2024 @03:27AM (#42212099)
Lua has rawget() and rawset() to bypass the magic methods, which is used a lot inside/outside the metatables (objects with magic methods) to avoid magic loops I guess.
Re:PHP 8.4 by Drew_ on Thursday November 21, 2024 @06:15PM (#42209674)
C# has the same thing as Properties, but the backing value is set explicitly as a separate variable:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-...

Re:PHP 8.4 by wvenable on Thursday November 21, 2024 @06:34PM (#42209785)
There are many times I would love to have an automatic backing field in C#. C# will do that if you use the default setter but in cases where that's not possible I dislike having to both declare a separate field and having it available to the rest of the class outside of the setter.
Re:PHP 8.4 by bwoebi on Thursday November 21, 2024 @06:38PM (#42209808)
C# 13 has this via the field keyword (as preview feature): https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...

It's pretty similar to what PHP provides here, except that PHP uses "$this->propname =" and C# uses "field =".

Edit: As someone involved in the RFC, it's somewhat funny, because we considered a special variable like "$field" or "$value" too magic, and C# does just that with a field keyword.

Re:PHP 8.4 by kroltan on Thursday November 21, 2024 @08:42PM (#42210425)
The `field` keyword also already existed in C#, to add attributes to the backing field of automatic properties, so I think the argument was easier there.

I used it in Unity projects to have serialized/inspectable values exposed through properties:

    [field: SerializeField]
    public int MyProperty { get; private set; }
Re:PHP 8.4 by Spivak on Friday November 22, 2024 @12:49AM (#42211474)
I think it being a keyword does change how magic the feature feels. "$field" and "$this->name" make me think that this behavior is playing by the normal rules of the language but is implemented using some trickery to edit the local scope of this function to add or modify variables. When it's a keyword it says this is a special form and that this is a feature of the language itself.
Re:PHP 8.4 by neonsunset on Thursday November 21, 2024 @06:37PM (#42209797)
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/cs...

edit: the properties reference now documents the 'field' keyword too

Re:PHP 8.4 by wvenable on Thursday November 21, 2024 @06:40PM (#42209819)
That might be all the motivation I need to go move to .NET 9.
Re:PHP 8.4 by runevault on Friday November 22, 2024 @12:17AM (#42211378)
.NET 9 has so much good stuff in it. Thing that I randomly realized I wanted only to find out it works in 9 is dictionary access with "related" types (so like if you have a ReadOnlySpan<char> you can use that to find a value from a string in the dictionary via a value type created off the dict).
Re:PHP 8.4 by nedt on Thursday November 21, 2024 @06:39PM (#42209818)
The dynamic form is really not needed very often. Should it ever become a problem triggering a loop you can just set and get from a private property with a different name. But then also 90% of getters and setters are really simple so it will feel as magic as gravity.
Re:PHP 8.4 by akie on Friday November 22, 2024 @01:55AM (#42211695)
Whenever I read about a new PHP release on HackerNews I always come away disappointed with the discussion. You are right, the "magic" that the top comment is complaining about is a non-issue, and I say that as someone who really strongly dislikes "magic" behavior.

In fact, you can imagine what their argument WOULD have been if setting `$this->countryCode` inside the setter for `countryCode` DID result in infinite recursion.

When it comes to PHP, whatever it does, on HackerNews and in the rest of the industry, it just results in a bunch of people complaining about it. Disappointing, and unprofessional.

Re:PHP 8.4 by ezekiel68 on Thursday November 21, 2024 @08:47PM (#42210444)
Funny world where a feature to prevent infinite recursion -- at a moment in the runtime lifecycle where that would make sense -- is somehow viewed as dangerous footgun magic (addresed to: several replies under this comment).
Re:PHP 8.4 by Dylan16807 on Thursday November 21, 2024 @08:58PM (#42210503)
You could prevent the recursion without this magic, by making the access a compiler error.
Re:PHP 8.4 by Implicated on Friday November 22, 2024 @12:19AM (#42211384)
404 ComPHPiler Not Found
Re:PHP 8.4 by Dylan16807 on Friday November 22, 2024 @02:44PM (#42216798)
Whatever you want to call the code that parses the file and emits a syntax error.

Though what would you personally call "the thing that turns it into bytecode"?

Re:PHP 8.4 by stephenr on Thursday November 21, 2024 @08:59PM (#42210506)
The original RFC that led to this feature originally used a special variable to indicate the backing store; that was overwhelmingly disliked, as it was "magic".

Conversely php already has other places where context affects property accesses, and IME it's not the problem you make it out to be.

Re:PHP 8.4 by hipadev23 on Thursday November 21, 2024 @09:02PM (#42210511)
> too much magic

The backing value is effectively private to everything but its own get/set methods, that seems fairly straightforward to me.

Re:PHP 8.4 by eurleif on Friday November 22, 2024 @02:10AM (#42211762)
What's magic is that `$this->foo` refers to different entities in different contexts despite the same value of `$this`, not that one of the entities to which it refers can be private.