Live data from Hacker News

PHP 8.4

php.net

161–170 of 337 posts

Re: PHP 8.4

#161
post #88

I'm just a PHP programmer for work, but I worry about the orientation PHP has chosen. As French people say: better is the enemy of good (Le mieux est l'ennemi du bien). The two new language features bring a higher language complexity for dubious gains. I hope I won't have to work with these. Property hooks mean that some language magic will turn a property access into a call to methods. It implies that `$this->x` has…

I just checked the code samples from the article/post. Property hooks look awesome, they fix something that's my main pain point in PHP nowadays. All these getters and setters manually coded make it feel like Java. Just completely boring and unusable without some fancy IDE that types all that boilerplate. It is one great feature of C# that I'm glad PHP is adopting. This code is also easier to extend than the Java-lik…

While these are certainly a better option automatically generated default getters and setters have been pretty do-able through magic methods for a while now - and with the more robust reflection we now have access to they can be implemented in a safe manner. I'm still pretty happy to hear we're getting it as a baked in feature.

Re: PHP 8.4

#162

Earlier quoted context omitted.

I'd tried to put together an RFC years ago to introduce groovy-style accessors in PHP. $this->foo would look for a getFoo() method, and execute if it existed, or not if not. Felt like that was easier to reason about, fwiw, but I couldn't get it off the ground. Even then, there were multiple C#-style get/set proposals floating around, so this style seems to be the one more people like. Not a fan of the style, personal…

I'm not a fan of that kind of magic in my languages but such a thing was already easily doable in PHP. You could just have a base class that implements __get and __set so that $this->foo automatically calls $this->getFoo().

can't do that if you declare the properties on the class. __get only works for undefined properties.

Re: PHP 8.4

#163

Earlier quoted context omitted.

For a project like Wikipedia, stability and continuity are far more important than latest and greatest feature support; in fact, there's an argument to be made to avoid newer language features to ensure consistency, that is, if new features are used, they should be used everywhere at once. Else you end up with frankenstein code where every file could use a different style.

"Else you end up with frankenstein code where every file could use a different style." Yeah, this is a huge problem, but also, in the long run, inevitable. Plenty of Kernighan & Ritchie C code still out there...

Almost none: ANSI was over 30 years ago and enough of an improvement people switched. If you mean C89 then yes, loads of it.

Re: PHP 8.4

#164
post #94

Earlier quoted context omitted.

I had similar thoughts, but do appreciate the additional mb_ functions bringing multi byte support to some remaining functions. Also people should be coding defensively with things like “if not defined” when implementing their own global helper functions (or avoid doing that at all)

"if not defined" doesn't help, if your own `array_find` doesn't have the same signature and semantics than the new global then you're screwed. You'd want the opposite: overwrite it if it already exists in the global scope (dunno if that's easy / how that'd work in PHP)

> `array_find` doesn't have the same signature and semantics than the new global then you're screwed

Screwed? Just rename or move the function to a namespace in an IDE and it will update all your references?

Re: PHP 8.4

#165
post #140

Earlier quoted context omitted.

What a condescending point of view.

I don't think OP was trying to be condescending. Even if they were, they're still right. A lot of modern languages are designed with bad programmers in mind.

Wasn't trying to be, but it's my experience that people will deliberately assume the very worst intentions, behind whatever I write, so it's a losing proposition, trying to be circumspect.

I am, however, pretty against selling crap. If people pay for my work, or even if they don't (most work I do, these days, is free), they have the right to expect that I did good work.

I do run into quite a few folks that write crap, know they write crap, but don't care, and expect people to pay top dollar for their crap.

If that's a "condescending" attitude, then guilty as charged.

Re: PHP 8.4

#166
post #51
post #6

These days, I am super torn about what language to use for new web projects. PHP: - Easy to deploy: Upload files, done. - Easy to develop: Reload page, see changes. - Lots of HTTP tooling built in. - Fast. Python: - Great language. - Great code reuse system: Modules. - Nice framework: Django. - Less breaking changes in recent years.

> Easy to deploy: Upload files, done I know people love to say this, but does anyone realistically make websites or web apps that way? No, not really. Even with PHP there are frameworks, there is a package manager, there is version control, and there are deployment systems. Pretending that PHP developers are uploading a .php file to a shared hosting server (like in 2002) to suit the narrative feels disingenuous to me…

I do it all the time, thank you very much. For a simple web app I don't even need a framework - you can literally create a simple files/folder structure, include files, include folders and get stuff done. An yeah, you can also upload the files by FTP. For something more complicated I'd use WP or Code Igniter depending on the specific project. And then you can again just SFTP those files to the server.

Re: PHP 8.4

#167
post #135

I'm most excited for property hooks. Having getters and setters as part of the language syntax was something I dearly missed from my C# days, nearly two decades ago. In my projects I sometimes emulate getters and setters using `__get()` and `__set()` but that's heavy-handed and requires lots of PHPDoc annotation for type checking. Property hooks look awesome!

I'm curious, why do you like getters and setters? I know the textbook answer is so that every single possible property can become some mutable chain of events so you can swap names or side-effects without the caller knowing, but I've yet to find a use for that in real life. It just leads to builder patterns and other oddities like forced decorators like Java has everywhere. I felt like beans were the realization that…

They are very useful when modeling CRUD-dy objects, because you can lazy-load infrequently-accessed child object using getters.

It makes for a cleaner OOP-y interface in which the caller only cares about actually exposed properties and not methods to get and manipulate hidden properties.

IMHO using properties directly is a much more natural way to talk about objects, than having a bunch of methods to get properties. Getters and setters also help ensure that methods are only for changing an object's state, not getting an object's state. For example:

  class User{
      public ForumsPost $LastForumsPost{
          get{
              if(!isset($this->LastForumsPost)){
                  $this->LastForumsPost = ;
              }

              // Return the cached property so we don't keep hitting the database.
              return $this->LastForumsPost;
          }
      }
  }

  print($user->LastForumsPost->Title);
  print($user->LastForumsPost->PostId);

  // instead of...

  print(($user->GetLastForumsPost())->Title);
  print(($user->GetLastForumsPost())->PostId);

Re: PHP 8.4

#168

Earlier quoted context omitted.

I'm not a fan of that kind of magic in my languages but such a thing was already easily doable in PHP. You could just have a base class that implements __get and __set so that $this->foo automatically calls $this->getFoo().

can't do that if you declare the properties on the class. __get only works for undefined properties.

Well don't do that then. :)

Re: PHP 8.4

#170
post #135

I'm most excited for property hooks. Having getters and setters as part of the language syntax was something I dearly missed from my C# days, nearly two decades ago. In my projects I sometimes emulate getters and setters using `__get()` and `__set()` but that's heavy-handed and requires lots of PHPDoc annotation for type checking. Property hooks look awesome!

I'm curious, why do you like getters and setters? I know the textbook answer is so that every single possible property can become some mutable chain of events so you can swap names or side-effects without the caller knowing, but I've yet to find a use for that in real life. It just leads to builder patterns and other oddities like forced decorators like Java has everywhere. I felt like beans were the realization that…

The more I program the more I realize that we are all blind men feeling out an elephant.

It sounds like you got the gist but somehow you are in an area of programming where getter/setters aren't useful.

That's fine and okay. Part of growing up as a programmer is realizing your niche.

Most of the advice out there is deep, not broad. It's deeply connected with our niche and not necessarily broadly applicable.

Post reply on HN