Ancestors

Toot

Written by dseguy on 2024-12-26 at 15:12

A #PHP global constant redefinition is a simple warning.

A #PHP class constant redefinition is a full Fatal Error.

I am so used to the Fatal error, that I expected the global constant to behave the same. Shouldn't they?

[#]phptip #phptrick

https://php-tips.readthedocs.io/en/latest/tips/constant_redefinition.html

=> View attached media

=> More informations about this toot | More toots from dseguy@phpc.social

Descendants

Written by galvao|galvaoetibr@bsky.social on 2024-12-26 at 15:14

@dseguy That deserves a RFC

=> More informations about this toot | More toots from galvao@phpc.social

Written by dseguy on 2024-12-26 at 15:18

@galvao Indeed, you're right.

I would start with a legacy check: in particular, multiple inclusion of the same would be now a Fatal error.

And with autoload, it might be a tiny fraction of codes.

=> More informations about this toot | More toots from dseguy@phpc.social

Written by galvao|galvaoetibr@bsky.social on 2024-12-26 at 15:19

@dseguy Do you have karma to do it?

=> More informations about this toot | More toots from galvao@phpc.social

Written by dseguy on 2024-12-26 at 15:56

@galvao I can check (a lot) of code, and write the RFC. Most of the value would be here.

Then, find someone else to do it :) .

=> More informations about this toot | More toots from dseguy@phpc.social

Written by Sara Golemon on 2024-12-26 at 15:24

@dseguy Fanatical dedication to backwards compatibility, innit? I susect there was concern over breaking WP or something.

=> More informations about this toot | More toots from pollita@phpc.social

Written by Alerta! Alerta! on 2024-12-26 at 15:32

@pollita It would leave a lot of WP installations unusable. And hinder further upgrades...

Because blaming the shitty BC policy of the PHP-team is much easier than fixing shitty code and check whether a constant is already defined before redefining it...

/cc @dseguy

=> More informations about this toot | More toots from heiglandreas@phpc.social

Written by SKC on 2024-12-26 at 15:36

@heiglandreas @pollita @dseguy I’m not sure I’ve seen this particular code in wide spread use in WP. I could be wrong but it’s not a typical pattern in WP plugins or core.

=> More informations about this toot | More toots from skc@scottodon.com

Written by Alerta! Alerta! on 2024-12-26 at 15:38

@skc Checking whether a constant is already defined before defining it?

/cc @pollita @dseguy

=> More informations about this toot | More toots from heiglandreas@phpc.social

Written by SKC on 2024-12-26 at 17:03

@heiglandreas @pollita @dseguy yeah that’s the normal practice I’ve seen, checking to see if it’s defined before defining it

=> More informations about this toot | More toots from skc@scottodon.com

Written by Alerta! Alerta! on 2024-12-26 at 17:06

@skc I've seen enough logfiles that showed, that it is not handled like that everywhere 😁

/cc @pollita @dseguy

=> More informations about this toot | More toots from heiglandreas@phpc.social

Written by dseguy on 2024-12-26 at 17:41

@heiglandreas @skc @pollita I measure (very roughly) about 1 out of 3 project defining global constants. And even more checking their existence.

It might be worth checking a number of those usages, to see actual usage, it might be interesting.

=> More informations about this toot | More toots from dseguy@phpc.social

Written by Giuseppe Mazzapica on 2024-12-26 at 15:40

@heiglandreas @pollita @dseguy to be fair: WP core and most of plugins will not break for this. But WP has a configuration-by-constants approach, and people in their wp-config do whatever. So, yes, many WP installations might break, but not something that can be fixed in WP code.

=> More informations about this toot | More toots from gmazzap@phpc.social

Written by Alerta! Alerta! on 2024-12-26 at 15:42

@gmazzap

if (! defined('ABC')) {

define('ABC', xyz);

//possibly even

ABC=xyz;

}

Looks not too complicated... 🤔

/cc @pollita @dseguy

=> More informations about this toot | More toots from heiglandreas@phpc.social

Written by Sara Golemon on 2025-01-09 at 22:18

@gmazzap @heiglandreas @dseguy

Constants that aren't constant...

Ya know, PHP has a cool feature for that. They're called variables. Cause they vary.

=> More informations about this toot | More toots from pollita@phpc.social

Written by Sherri W (SyntaxSeed) on 2024-12-26 at 16:37

@heiglandreas @pollita @dseguy I've never figured out who these admins are that upgrade #PHP on the server but not #WordPress on the site. 🤔

=> More informations about this toot | More toots from syntaxseed@phpc.social

Written by Alerta! Alerta! on 2024-12-26 at 17:09

@syntaxseed Those that maintain shared hosting where users can install whatever they want?

And users then decide to not use the providers supplied oneclick installer... And also never update WP because "It'll break 😱"

I've seen things.... 🙈

/cc @pollita @dseguy

=> More informations about this toot | More toots from heiglandreas@phpc.social

Written by Sherri W (SyntaxSeed) on 2024-12-26 at 20:45

@heiglandreas @pollita @dseguy I use a lot of shared hosting. Never had the PHP version change from under me- typically I go to my control panel & can choose from versions currently available. I then have to flip the "switch" in order to upgrade PHP.

I feel like people who don't bother with WP upgrades aren't going to do either.

If the version changes from under you, either the host is awful or you ignored years of warnings (we've been getting 5.x discontinuation notices for many years. 🤣

=> More informations about this toot | More toots from syntaxseed@phpc.social

Written by Sherri W (SyntaxSeed) on 2024-12-26 at 20:46

@heiglandreas @pollita @dseguy But yeah... I'm sure the horror stories are everywhere!

I'm just not sure that "we" (PHP) should be shielding them. 😉

=> More informations about this toot | More toots from syntaxseed@phpc.social

Written by Alerta! Alerta! on 2024-12-26 at 21:02

@syntaxseed I hope you didn't get me wrong: We shouldn't!

/cc @pollita @dseguy

=> More informations about this toot | More toots from heiglandreas@phpc.social

Written by Sherri W (SyntaxSeed) on 2024-12-26 at 21:46

@heiglandreas @pollita @dseguy Nope, I was agreeing with you. 😊

=> More informations about this toot | More toots from syntaxseed@phpc.social

Written by galvao|galvaoetibr@bsky.social on 2024-12-26 at 22:31

@heiglandreas @pollita @dseguy We should, IMO, take advantage that we're already talking about PHP9 and make a RFC for it =)

=> More informations about this toot | More toots from galvao@phpc.social

Written by dseguy on 2024-12-26 at 15:58

@pollita Yes, I suspect backward compatibility.

Now, I'd like to run some SCA check to spot such problems, and see how it can be fixed (automatically? manually?).

That way, it might help everyone.

=> More informations about this toot | More toots from dseguy@phpc.social

Written by Gina Peter Banyard on 2024-12-26 at 16:05

@pollita @dseguy it used to be a notice in PHP 7 and was elevated to a warning in 8.

I'm sure we will just have another round of elevating warnings to errors in PHP 9.

=> More informations about this toot | More toots from Girgias@phpc.social

Written by Tobias van Beek on 2024-12-26 at 16:30

@dseguy I suspect it can be problematic if packages define global constance. (I don't know if there are packages who are doing that)

=> More informations about this toot | More toots from tvbeek@phpc.social

Written by dseguy on 2024-12-26 at 17:34

@tvbeek A quick search across 3100+ OSS source codes shows 902 (29%) of them using define(): even more of them use const, although for class constant too.

Then, 1370 uses defined() (44%): again, class and global, so actual might be lower.

So, that is roughly one out of 3 of us.

If it is not you, or me, who can it be? names! names! 😂

=> More informations about this toot | More toots from dseguy@phpc.social

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113719768147490983
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
400.422228 milliseconds
Gemini-to-HTML Time
7.952001 milliseconds

This content has been proxied by September (ba2dc).