PHP

696 readers
1 users here now

Welcome to /c/php! This is a community for PHP developers and enthusiasts to share and discuss anything related to PHP. From the latest updates and tutorials, to your burning questions and amazing personal projects, we welcome all contributions.

Let's foster an environment of respect, learning, and mutual growth. Whether you're an experienced PHP developer, a beginner, or just interested in learning more about PHP, we're glad to have you here!

Let's code, learn, and grow together!

founded 2 years ago
MODERATORS
1
 
 

cross-posted from: https://chrastecky.dev/post/16

Starting with PHP 8.5, you'll be able to do the following:

 public function __construct(
    final public string $someProperty,
) {}

This wasn't possible before, as promoted properties couldn't be declared final.

Perhaps the more interesting part is that you can now omit the visibility modifier if you include final. In that case, the property will default to public:

 public function __construct(
    final string $someProperty, // this property will be public
) {}

Personally, I’m not a fan of this behavior — I prefer explicit over implicit. Fortunately, it can be enforced by third-party tools like code style fixers. Still, I would have preferred if the core required the visibility to be specified.

What do you think? Do you like this change, or would you have preferred a stricter approach?

2
 
 

cross-posted from: https://chrastecky.dev/post/13

This change is quite straightforward, so this won’t be a long article. PHP 8.5 adds support for annotating non-class, compile-time constants with attributes. Compile-time constants are those defined using the const keyword, not the define() function.

Attributes can now include Attribute::TARGET_CONSTANT among their valid targets. Additionally, as the name suggests, Attribute::TARGET_ALL now includes constants as well. The ReflectionConstant class has been updated with a new method, getAttributes(), to support retrieving these annotations.

One particularly useful aspect of this change is that the built-in #[Deprecated] attribute can now be applied to compile-time constants.

As promised, this was a short post, since the change is relatively simple. See you next time—hopefully with a more exciting new feature in PHP 8.5!

3
 
 

cross-posted from: https://chrastecky.dev/post/15

PHP has long had a levenshtein() function, but it comes with a significant limitation: it doesn’t support UTF-8.

If you’re not familiar with the Levenshtein distance, it’s a way to measure how different two strings are — by counting the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another.

For example, the following code returns 2 instead of the correct result, 1:

var_dump(levenshtein('göthe', 'gothe'));

There are workarounds — such as using a pure PHP implementation or converting strings to a custom single-byte encoding — but they come with downsides, like slower performance or non-standard behavior.

With the new grapheme_levenshtein() function in PHP 8.5, the code above now correctly returns 1.

Grapheme-Based Comparison

What makes this new function especially powerful is that it operates on graphemes, not bytes or code points. For instance, the character é (accented 'e') can be represented in two ways: as a single code point (U+00E9) or as a combination of the letter e (U+0065) and a combining accent (U+0301). In PHP, you can write these as:

$string1 = "\u{00e9}";
$string2 = "\u{0065}\u{0301}";

Even though these strings are technically different at the byte level, they represent the same grapheme. The new grapheme_levenshtein() function correctly recognizes this and returns 0 — meaning no difference.

This is particularly useful when working with complex scripts such as Japanese, Chinese, or Korean, where grapheme clusters play a bigger role than in Latin or Cyrillic alphabets.

Just for fun: what do you think the original levenshtein() function will return for the example above?

var_dump(levenshtein("\u{0065}\u{0301}", "\u{00e9}"));
4
1
PHP 8.3.22 (www.php.net)
submitted 3 weeks ago* (last edited 3 weeks ago) by cm0002@lemmy.world to c/php@programming.dev
5
6
7
8
9
 
 

June 17, 2025

  • 13:30–19:00 CET/CEST
  • 07:30–13:00 EST/EDT
  • 11:30–17:00 UTC
10
 
 

Internals link: https://externals.io/message/127120

Note: I am not the RFC author.

11
 
 

Note: I am not the RFC author.

12
13
14
 
 

I noticed that if you have too few pm_children set then some requests hang until timeout. This surprised me - I'd expect an immediate error, but it's more like a tarpit! For ages I was thinking my server was not performant, then I noticed via top that it wasn't doing or waiting while the browser was.

I have two questions:

  1. If you have pm_max_children=1 and you occupy that and submit another request, what actually happens? (I'm proxying through nginx.) HTTP doesn't have a "40_ Come back later".

  2. (if life deals you lemons...) if you can generate a tarpit that doesn't use server resources, this could be quite useful to know about too!

15
 
 

A summary of the highlights and key accomplishments of the Symfony project in 2024.

16
 
 

My TLDR is:

  • Their team was using PHP

  • Before doing a complete re-write they evaluated other languages

  • Rust ruled out due to cost/benefit, being the fastest in the list, but also the most complex

  • PHP kept as the main language because:

    • The ecosystem is mature
    • The PHP/Symfony (and Roadrunner) stack meets their high-performance needs
  • Inertia: their team "already had extensive experience" in it

  • They already integrated Go in some microservices

  • They aren't locked to PHP, and will continue to evaluate these programming languages and others

17
 
 

Ever wanted to provide your arguments to a function as a comment?

https://gist.github.com/RikudouSage/18defbf1746322a289ae78b2980d0115

#php #cursed #wtf #programming @php

18
19
9
submitted 8 months ago* (last edited 8 months ago) by psion1369@lemmy.world to c/php@programming.dev
 
 

I've been working on this on and off, could use some other eyes to help expand features, maybe some pull requests. Let me know what you think. I just uploaded version 0.0.12 and I am going to start working on 0.0.13. The CMS is built in a bundle, so that's where most of the features are. What I have linked here is the skeleton app, but installation is still beefy. Thanks everyone!

Edit: Forgot the link to the bundle https://github.com/SeleneSoftware/seleneCMSBundle

20
 
 

Should I create functions for packages/libraries that allow optional parameters to accept null?

In this example below, I set the 3rd and 4th parameter as null which will act as the default value.

myLibrary::myFunction(1, 7, null, null, true);

Or is this not a good way to go about creating functions for a package and therefore should not accept null as a parameter value.

myLibrary::myFunction(1, 7, false, 4, true);
21
22
23
 
 

I want to try to migrate from PHPStorm to VSCode, what plugins to get to have the experience similar? I mean the code inspections, renaming, usage etc. Will PHL Intelephense plugin be all I need to achieve this? with xdebug and phpunit plugins in addition to that, or there are some other plugins that make the transition better?

24
25
view more: next ›