# [RFC] Deprecations for PHP 8.4

- Thread: https://externals.io/message/123816
- Original archive: https://news-web.php.net/php.internals/123816
- Started by Gina P. Banyard on 2024-06-25T14:36:53+00:00
- Messages: 68
- RFC: Deprecations for PHP 8.4 (Accepted) — https://externals.io/rfc/deprecations_php_8_4.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/123816.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #123816 — Gina P. Banyard — 2024-06-25T14:36:53+00:00

URL: https://externals.io/message/123816#123816

Hello internals,

It is this time of year again where we proposed a list of deprecations to add in PHP 8.4:

https://wiki.php.net/rfc/deprecations_php_8_4

As a reminder, this list has been compiled over the course of the past year by various different people.

And as usual, each deprecation will be voted in isolation.

We still have a bit of time buffer, so if anyone else has any suggestions, they are free to add them to the RFC.

Some should be non-controversial, others a bit more.
If such, they might warrant their own dedicated RFC, or be dropped from the proposal altogether.


Best regards,

Gina P. Banyard

---

## #123818 — Marco Pivetta — 2024-06-25T15:27:20+00:00

URL: https://externals.io/message/123816#123818 · In reply to: #123816

Hey Gina, Tim,

I agree with most of these deprecations, except:

 * `uniqid()`, in my case (XKCD 1172) is largely used for quickly
generating a semi-random string for test purposes: a suitable replacement
PRNG implementation would be welcome. Even refactoring with tools like
Rector will lead to quite messy code, or added dependencies. IMO fine to
get rid of **this specific implementation**, if a safe function is
provided, such as `random_ascii_string()` or such (dunno, just a hint)
 * `md5()`, `sha1()` - OK-ish with moving to `hash('<algo>', ...)`, but
while these are insecure for most use-cases, they are part of the domain of
many tools, including GIT itself. I can Rector my way out of it, just not
sure these should be hidden into `hash(...)`

That said, welcome changes :-)

Marco Pivetta

https://mastodon.social/@ocramius

https://ocramius.github.io/




> Hello internals,
>
> It is this time of year again where we proposed a list of deprecations to
> add in PHP 8.4:
>
> https://wiki.php.net/rfc/deprecations_php_8_4
>
> As a reminder, this list has been compiled over the course of the past
> year by various different people.
>
> And as usual, each deprecation will be voted in isolation.
>
> We still have a bit of time buffer, so if anyone else has any suggestions,
> they are free to add them to the RFC.
>
> Some should be non-controversial, others a bit more.
> If such, they might warrant their own dedicated RFC, or be dropped from
> the proposal altogether.
>
>
> Best regards,
>
> Gina P. Banyard

---

## #123820 — Rob Landers — 2024-06-25T15:39:35+00:00

URL: https://externals.io/message/123816#123820 · In reply to: #123816

> Hello internals,
> 
> It is this time of year again where we proposed a list of deprecations to add in PHP 8.4:
> 
> https://wiki.php.net/rfc/deprecations_php_8_4
> 
> As a reminder, this list has been compiled over the course of the past year by various different people.
> 
> And as usual, each deprecation will be voted in isolation.
> 
> We still have a bit of time buffer, so if anyone else has any suggestions, they are free to add them to the RFC.
> 
> Some should be non-controversial, others a bit more.
> If such, they might warrant their own dedicated RFC, or be dropped from the proposal altogether.
> 
> 
> Best regards,
> 
> Gina P. Banyard
> 

My only issue is md5, sha1, etc. There are many uses for them besides secure contexts. Sha1 is used by git, md5 fits snuggly in many data structures (uuidv5, for example, though some implementations also use the first 128 bits of a sha1).

Even though these may be cryptographically weak, they are quite useful and well understood. 

— Rob

---

## #123821 — Derick Rethans — 2024-06-25T15:49:36+00:00

URL: https://externals.io/message/123816#123821 · In reply to: #123818

Please, no top posting.



> I agree with most of these deprecations, except:
> 
>  * `uniqid()`, in my case (XKCD 1172) is largely used for quickly
> generating a semi-random string for test purposes: a suitable replacement
> PRNG implementation would be welcome. Even refactoring with tools like
> Rector will lead to quite messy code, or added dependencies. IMO fine to
> get rid of **this specific implementation**, if a safe function is
> provided, such as `random_ascii_string()` or such (dunno, just a hint)

>  * `md5()`, `sha1()` - OK-ish with moving to `hash('<algo>', ...)`, but
> while these are insecure for most use-cases, they are part of the domain of
> many tools, including GIT itself. I can Rector my way out of it, just not
> sure these should be hidden into `hash(...)`

I agree on both points.

These functions are indeed often used. The documentation for uniqid has 
some nice big warnings too. It can be used in situations, where it is 
OK.

Replacing the algorithm underneath uniqid can (and perhaps should) be 
looked at.

For the md5 and sha1 functions. Although these algorithms are not useful 
for "verifying the authenticity of a a payload", that doesn't mean they 
can't be used outside of that use case. 

I understand the reasons why you want to "nudge" people to no longer use 
these "unsafe" functions, but IMO, in this case, it's not worth the 
BC issues.

cheers,
Derick

---

## #123823 — Marco Pivetta — 2024-06-25T15:55:04+00:00

URL: https://externals.io/message/123816#123823 · In reply to: #123818

>  * `uniqid()`, in my case (XKCD 1172) is largely used for quickly
> generating a semi-random string for test purposes: a suitable replacement
> PRNG implementation would be welcome. Even refactoring with tools like
> Rector will lead to quite messy code, or added dependencies. IMO fine to
> get rid of **this specific implementation**, if a safe function is
> provided, such as `random_ascii_string()` or such (dunno, just a hint)
>

Update: Tim gave me a decent alternative that I can live with.

`uniqid()` becomes `bin2hex(random_bytes(16))`.

I can live with that :-)

Marco Pivetta

https://mastodon.social/@ocramius

https://ocramius.github.io/

---

## #123825 — Rowan Tommins [IMSoP] — 2024-06-25T16:03:02+00:00

URL: https://externals.io/message/123816#123825 · In reply to: #123818

>
>  * `uniqid()`, in my case (XKCD 1172) is largely used for quickly 
> generating a semi-random string for test purposes: a suitable 
> replacement PRNG implementation would be welcome. Even refactoring 
> with tools like Rector will lead to quite messy code, or added 
> dependencies. IMO fine to get rid of **this specific implementation**, 
> if a safe function is provided, such as `random_ascii_string()` or 
> such (dunno, just a hint)


Agreed, the implementation is weird, but nothing else matches the 
convenience to just get "some random printable bytes". As of PHP 8.3, we 
finally have Random\Randomizer::getBytesFromString, but the comparison 
is pretty stark:

$foo = `uniqid()`;
$foo = (new 
\Random\Randomizer)->getBytesFromString('abcdefghijklmnopqrstuvwxyz0123456789', 
13);

Alternatively, you have the shorter but slightly cryptic:

$foo = bin2hex(random_bytes(6));

Then again, if you _actually_ want it to be unique, rather than random, 
those aren't the right replacements anyway.

I'd love to replace `uniqid()` with *something*, but I don't think we have 
that thing yet.


-- 
Rowan Tommins
[IMSoP]

---

## #123830 — tim@bastelstu.be — 2024-06-25T17:43:14+00:00

URL: https://externals.io/message/123816#123830 · In reply to: #123823

Hi


> Update: Tim gave me a decent alternative that I can live with.
> 
> `uniqid()` becomes `bin2hex(random_bytes(16))`.

For context: Marco also pinged me on Roave Discord and I sent a quick 
reply from my phone.

I've now added the `bin2hex(random_bytes(16))` alternative to the RFC 
text: 
https://wiki.php.net/rfc/deprecations_php_8_4?do=diff&rev2%5B0%5D=1719336981&rev2%5B1%5D=1719337102&difftype=sidebyside

Best regards
Tim Düsterhus

---

## #123832 — tim@bastelstu.be — 2024-06-25T17:58:37+00:00

URL: https://externals.io/message/123816#123832 · In reply to: #123825

Hi


> Then again, if you _actually_ want it to be unique, rather than random,
> those aren't the right replacements anyway.

They are for all intents and purposes if the generated string is long 
enough. By the pigeonhole principle you can't guarantee uniqueness for a 
fixed-length string, but when you have 128 bits of entropy you are 
statistically all but guaranteed to receive a unique string. I've made 
an example calculation for the "session.sid_length and 
session.sid_bits_per_character" bit of this very RFC.

The replacement I suggested to Marco `bin2hex(random_bytes(16))` does 
use exactly 128 bits (16 bytes) of secure randomness for that reason.

For Randomizer::getBytesFromString() you can calculate the entropy as 
follows:

     var_dump(log(strlen($string) ** $length, 2));

You can calculate the minimum length to have 128 bits of entropy for a 
given alphabet string as follows:

     var_dump(ceil(log(2**128, strlen($string))));

For some example alphabets, the minimum length for 128 bits of entropy 
would be:

- [0-9]      : 39
- [0-9a-f]   : 32
- [a-z]      : 28
- [a-z0-9]   : 25
- [a-zA-Z]   : 23
- [a-zA-Z0-9]: 22

Best regards
Tim Düsterhus

---

## #123833 — Mike Schinkel — 2024-06-25T18:06:17+00:00

URL: https://externals.io/message/123816#123833 · In reply to: #123816

> 
> 
> Hello internals,
> 
> It is this time of year again where we proposed a list of deprecations to add in PHP 8.4:
> 
> https://wiki.php.net/rfc/deprecations_php_8_4 <https://wiki.php.net/rfc/deprecations_php_8_4>
> 
> As a reminder, this list has been compiled over the course of the past year by various different people.
> 
> And as usual, each deprecation will be voted in isolation.
> 
> We still have a bit of time buffer, so if anyone else has any suggestions, they are free to add them to the RFC.
> 
> Some should be non-controversial, others a bit more.

 `strtok()`
=====
 `strtok()` is found 35k times in GitHub:

https://github.com/search?q=md5%28+language%3APHP+&type=code <https://github.com/search?q=md5%28+language%3APHP+&type=code>

It is a commonly used as a "left part of string up to a character" in addition to its intended use for tokenizing.  

I would prefer not deprecated because of BC breakage, but IF it is deprecated I would suggest adding a one-for-one replacement function for the  "left part of string up to a character" use-case; maybe `str_left("abc.txt",".")` returning `"abc"`.


 `md5()`/md5_file()
=============
Just FYI, `md5()` is found 868k times and `md5_file()` 29.7k times in GitHub:

https://github.com/search?q=md5%28+language%3APHP+&type=code <https://github.com/search?q=md5%28+language%3APHP+&type=code>
https://github.com/search?q=md5_file%28+language%3APHP+&type=code <https://github.com/search?q=md5_file%28+language%3APHP+&type=code>

That is a lot or broken code.

However, if deprecated I would suggest adding `insecure_md5()` and `insecure_md5_file()`  as a drop-in replacement which would be more obvious and easier than using `hash()` — so people would be more apt to use it — and that would signal they are obviously using an insecure function which increases the likelihood developers to go to the effort to actually fix the security issues in their code and/or not use md5 for security sensitive code to begin with.

 `sha1()`/sha1_file()
=============
 `sha1()` is found 167k times and `sha1_file()` 6.8k times in GitHub:

https://github.com/search?q=sha1%28+language%3APHP+&type=code <https://github.com/search?q=sha1%28+language%3APHP+&type=code>
https://github.com/search?q=sha1_file%28+language%3APHP+&type=code <https://github.com/search?q=sha1_file%28+language%3APHP+&type=code> 

Same arguments for `md5()`/md5_file(), e.g. if deprecated add  `insecure_sha1()` and `insecure_sha1_file().

#jmtcw

-Mike

---

## #123835 — tim@bastelstu.be — 2024-06-25T18:08:13+00:00

URL: https://externals.io/message/123816#123835 · In reply to: #123820

Hi


> My only issue is md5, sha1, etc. There are many uses for them besides secure contexts. Sha1 is used by git, md5 fits snuggly in many data structures (uuidv5, for example, though some implementations also use the first 128 bits of a sha1).

I believe you might have misunderstood the proposal. The RFC is not 
proposing the MD5 and SHA-1 *algorithms*. It's just proposing the 
standalone functions.

The algorithms will still be available by means of the `hash()` function, 
which - besides MD5 and SHA-1 - provides access to a multitude of other 
hash algorithms. The RFC explicitly lists the necessary replacement.

The RFC is just proposing phasing out the special treatment of having 
dedicated functions of MD5 and SHA-1.

Best regards
Tim Düsterhus

PS: git is planning to move away from SHA-1.

---

## #123836 — tim@bastelstu.be — 2024-06-25T18:15:31+00:00

URL: https://externals.io/message/123816#123836 · In reply to: #123821

Hi


> These functions are indeed often used. The documentation for uniqid has
> some nice big warnings too. It can be used in situations, where it is
> OK.

Yes, the RFC acknowledges that.

> Replacing the algorithm underneath uniqid can (and perhaps should) be
> looked at.

As explained in the RFC: The behavior is unfixable, because `uniqid()` is 
documented to be time-based. Changing the output format would be a much 
bigger breaking change than deprecating the function and letting users 
make an educated decision when choosing an alternative.

> For the md5 and sha1 functions. Although these algorithms are not useful
> for "verifying the authenticity of a a payload", that doesn't mean they
> can't be used outside of that use case.

See my reply to Robert. The algorithms themselves are not going away. At 
least not as part of this RFC.

> I understand the reasons why you want to "nudge" people to no longer use
> these "unsafe" functions, but IMO, in this case, it's not worth the
> BC issues.
> 

For both `uniqid()` and the `md5()`/sha1() section, the RFC specifically 
acknowledges that there is a vast collection of code using it and for 
this reason specifically defines an indefinite deprecation period (with 
a guarantee that indefinite is at least 5 years / until PHP 10).

Please keep in mind that a deprecation is not an error, not a removal 
and not a change in behavior. Users will be able to migrate on their own 
pace and for both `uniqid()` and `md5()`/sha1() there is a drop-in 
replacement provided in the RFC that is guaranteed to work with PHP 7.4+.

Best regards
Tim Düsterhus

---

## #123844 — Gina P. Banyard — 2024-06-25T20:51:45+00:00

URL: https://externals.io/message/123816#123844 · In reply to: #123833

> `strtok()`
> =====
> `strtok()` is found 35k times in GitHub:
>
> > […]
>
> It is a commonly used as a "left part of string up to a character" in addition to its intended use for tokenizing.
>
> I would prefer not deprecated because of BC breakage, but IF it is deprecated I would suggest adding a one-for-one replacement function for the "left part of string up to a character" use-case; maybe `str_left("abc.txt",".")` returning `"abc"`.

For this exact case of extracting a file name without an extension, you should really just use:

pathinfo

(

$filepath

, PATHINFO_FILENAME);

But for something more generic, you can just do:
explode($delimiter, $str)[0];

So I really don't see why we would need an "str_left()" function.

Best regards,
Gina P. Banyard

---

## #123852 — Bruce Weirdan — 2024-06-25T22:29:52+00:00

URL: https://externals.io/message/123816#123852 · In reply to: #123816

Is there a reason to keep crc32?

-- 
  Best regards,
      Bruce Weirdan                                     mailto:
weirdan@gmail.com

---

## #123854 — Mike Schinkel — 2024-06-26T05:18:16+00:00

URL: https://externals.io/message/123816#123854 · In reply to: #123844

> 
> 
> 
> 
> > […]
> 
> 
> For this exact case of extracting a file name without an extension, you should really just use:
> pathinfo($filepath, PATHINFO_FILENAME);
> But for something more generic, you can just do:
> explode($delimiter, $str)[0];
> 
> So I really don't see why we would need an "str_left()" function.

Ah, the dangers of providing a specific example of a broader use-case is that someone will invariably discredit the specific example instead of focusing on the applicability for the broader use-case. 🤦‍♂️

To wit, here are seven (7) use-cases for which `pathinfo()` is not a viable alternative:

https://3v4l.org/RDYFs#v8.3.8 <https://3v4l.org/RDYFs#v8.3.8>

Note those seven use-cases are found in around the first 25 results when searching GitHub for "strtok(".  I could probably find more if I kept looking:

https://github.com/search?q=strtok%28+language%3APHP+&type=code <https://github.com/search?q=strtok%28+language%3APHP+&type=code>

Regarding explode($delimiter, $str)[0] — unless it is to be special-cased during compilation —it is a really inefficient way to find the substring up to the first character, especially for large strings and/or when in a tight loop where the explode is contained in a called function.

Here is a benchmark (https://onlinephp.io/c/87341) showing that — on average of the runs I performed — for using `strtok()` to fully process through a 3972 byte file with 359 commas it took right at 90 times longer using explode($delimiter, $str)[0] vs. strtok($str,$delimiter). Imagine is the file were 39,720 bytes, or larger, instead. 

Size of file:                3972
Number of commas:            359
Time taken for strtok:       0.0034 seconds
Time taken for explode:      0.3036 seconds
Times `strtok()` faster:     89.1

Yes the above processes the entire file using `explode()`[0] each time rather than first using explode(",") once — because of the equivalent of the N+1 problem[1] where the `explode()` is buried in a function. This illustrates why `strtok()` is so good for its primary use-case of parsing text files. `strtok()` is fast and does not use heaps of memory on every token. 

This leads me to think `strtok()` should not be deprecated given how inefficient string handling in PHP can otherwise be, at least not without a much more efficient object for string parsing.

-Mike
[1] https://www.baeldung.com/cs/orm-n-plus-one-select-problem <https://www.baeldung.com/cs/orm-n-plus-one-select-problem

---

## #123855 — Dusk — 2024-06-26T05:39:08+00:00

URL: https://externals.io/message/123816#123855 · In reply to: #123854

> This leads me to think `strtok()` should not be deprecated given how inefficient string handling in PHP can otherwise be, at least not without a much more efficient object for string parsing.

What would be really useful as a replacement for `strtok()` - among other things - would be a function analogous to MySQL's SUBSTRING_INDEX():

https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_substring-index

Where SUBSTRING_INDEX($a, $b, $c) is functionally equivalent to explode($a, $b)[$c], but with the added ability to use negative indices to count from the end of the input.

---

## #123858 — Mike Schinkel — 2024-06-26T06:10:30+00:00

URL: https://externals.io/message/123816#123858 · In reply to: #123855

> 
> 
> 
> > […]
> 
> What would be really useful as a replacement for `strtok()` - among other things - would be a function analogous to MySQL's SUBSTRING_INDEX():
> 
> https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_substring-index
> 
> Where SUBSTRING_INDEX($a, $b, $c) is functionally equivalent to explode($a, $b)[$c], but with the added ability to use negative indices to count from the end of the input.

Yes. There are numerous quality-of-life functions like that which would improve PHP DX, performance, and likely security if incorporated into the standard library. 

Unfortunately there is a generally antipathy on this list towards adding functions that "can be written in userland" even though relegating them to userland means many people writing, writing about and publishing many different named functions doing similar and often incompatible things, and doing them less efficiently than if the one-time bullet was bitten and they were written in C, added to the docs, and included in core PHP. 

#fwiw

-Mike

P.S. And no, `SUBSTRING_INDEX($a, $b, $c)` would not add a significant maintenance burden. Simple functions are an order of magnitude easier to maintain than, for example, adding new syntax for new language features, or adding a library feature needs to be upgraded in response to an evolution orthogonal to PHP, such as supporting a file format, a protocol or database connector.

---

## #123865 — Gina P. Banyard — 2024-06-26T12:14:03+00:00

URL: https://externals.io/message/123816#123865 · In reply to: #123854

> > […]
>
> Note those seven use-cases are found in around the first 25 results when searching GitHub for "strtok(". I could probably find more if I kept looking:
>
> > […]
>
> > […]
>
> Regarding explode($delimiter, $str)[0] — unless it is to be special-cased during compilation —it is a really inefficient way to find the substring up to the first character, especially for large strings and/or when in a tight loop where the explode is contained in a called function

Then use a regex: https://3v4l.org/SGWL5

Or a combination of strpos and substr.

I'd bet that both of these solutions would use less memory, and I would guess the PCRE one should also be better for performance (although not benchmarked) as it is highly specialized in that task.

There are *plenty* of solutions to the specific problem you pose here, and thus many different solutions more or less appropriate.

Best regards,
Gina P. Banyard

---

## #123866 — Gina P. Banyard — 2024-06-26T12:17:31+00:00

URL: https://externals.io/message/123816#123866 · In reply to: #123855

> 
> 
> > […]
> 
> 
> What would be really useful as a replacement for `strtok()` - among other things - would be a function analogous to MySQL's SUBSTRING_INDEX():
> 
> https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_substring-index
> 
> Where SUBSTRING_INDEX($a, $b, $c) is functionally equivalent to explode($a, $b)[$c], but with the added ability to use negative indices to count from the end of the input.

That is a rather interesting function that I did not know existed in MySQL.
I agree this would be useful, and probably should be its own RFC/thread.

Best regards,

Gina P. Banyard

---

## #123869 — Kamil Tekiela — 2024-06-26T13:54:11+00:00

URL: https://externals.io/message/123816#123869 · In reply to: #123816

I think the "Deprecate passing `E_USER_ERROR` to `trigger_error()`" should
be better explained. Why is using this constant a problem? There is a
link to another RFC, but I can't see an explanation as to why
 `E_USER_ERROR` suffers the same problem as fatal errors do. From an
average Joe's perspective, it looks fine and does the job
https://3v4l.org/e97TO

---

## #123882 — Morgan — 2024-06-26T19:19:27+00:00

URL: https://externals.io/message/123816#123882 · In reply to: #123816

I do not believe it is appropriate to deprecate `strtok()` without a proper
replacement.

While I agree that its signature is undesirable, the suggested replacement
functions or “just write a parser” are not very pleasant solutions to fill
the void it would leave.

The stateful functionality it exhibits is incredibly useful, though I will
admit confusing. Would it not be better to change how the functionality is
accessed to reflect the fact that state is preserved rather than remove it
entirely and force a performance burden on developers?



> Hello internals,
>
> It is this time of year again where we proposed a list of deprecations to
> add in PHP 8.4:
>
> https://wiki.php.net/rfc/deprecations_php_8_4
>
> As a reminder, this list has been compiled over the course of the past
> year by various different people.
>
> And as usual, each deprecation will be voted in isolation.
>
> We still have a bit of time buffer, so if anyone else has any suggestions,
> they are free to add them to the RFC.
>
> Some should be non-controversial, others a bit more.
> If such, they might warrant their own dedicated RFC, or be dropped from
> the proposal altogether.
>
>
> Best regards,
>
> Gina P. Banyard

---

## #123915 — Gina P. Banyard — 2024-06-27T03:46:28+00:00

URL: https://externals.io/message/123816#123915 · In reply to: #123882

> I do not believe it is appropriate to deprecate `strtok()` without a proper replacement.
>
> While I agree that its signature is undesirable, the suggested replacement functions or “just write a parser” are not very pleasant solutions to fill the void it would leave.
>
> The stateful functionality it exhibits is incredibly useful, though I will admit confusing. Would it not be better to change how the functionality is accessed to reflect the fact that state is preserved rather than remove it entirely and force a performance burden on developers?

First of all, please do not top post on the mailing list.

Secondly, please explain how you would provide the statefullness.
Thirdly please provide an example of usage of `strtok()` where the suggestions I have given as replies in this thread are not applicable.
If the problem is indeed parsing some very complicated structure and you are using strtok for this, I would argue writing a proper parser is overall better.

Of note, I'm not the direct author of this section, I just cleaned it up because the initial wording was... draft state like.

Best regards,
Gina P. Banyard

---

## #123916 — Gina P. Banyard — 2024-06-27T03:57:51+00:00

URL: https://externals.io/message/123816#123916 · In reply to: #123869

> I think the "Deprecate passing `E_USER_ERROR` to `trigger_error()`" should
> be better explained. Why is using this constant a problem? There is a
> link to another RFC, but I can't see an explanation as to why
> `E_USER_ERROR` suffers the same problem as fatal errors do. From an
> average Joe's perspective, it looks fine and does the job
> https://3v4l.org/e97TO

Returning control after an `E_USER_ERROR` seems problematic to me in
the first place, as the condition which lead to the trigger surely
implies the current code is unable to handle the situation.
See: https://3v4l.org/7pdvO

But the issues with fatal errors are the same as explained in the
linked RFC, in that destructors (and finally blocks, etc.) are not
called. See: https://3v4l.org/J5NXF

Using exceptions instead is more robust.
Is this explanation clear enough?
If so, I will incorporate it into the RFC.

Best regards,

Gina P. Banyard

---

## #123917 — Gina P. Banyard — 2024-06-27T04:01:04+00:00

URL: https://externals.io/message/123816#123917 · In reply to: #123852

> Is there a reason to keep crc32?

Good question, I had a chat with Tim as I thought it was similar to the `md5()`/sha1() functions.
Moreover, the `crc32()` function returns an int, whereas the equivalent of the hash extension
returns a string, so to get the same behaviour one needs to do:

hexdec(hash('crc32b', $str));

It might still make sense to add it to the RFC, but it would need to be its own section with
its own rationale.

Best regards,
Gina P. Banyard

---

## #123925 — Mike Schinkel — 2024-06-27T05:31:44+00:00

URL: https://externals.io/message/123816#123925 · In reply to: #123865

> 
> 
> 
> 
> > […]
> 
> Then use a regex: https://3v4l.org/SGWL5 <https://3v4l.org/SGWL5>

Using `preg_match()` instead of `strtok()` to process the ~4k file of commas is, on average, same as using `explode()`[0], or 10x as long as using `strtok()` (at times it got as low as 4.4x, but that was rare):

https://onlinephp.io/c/e1fad <https://onlinephp.io/c/e1fad>

Size of file:          3972
Number of commas:      359
Time taken for strtok: 0.003 seconds
Time taken for regex:  0.0307 seconds
Times `strtok()` faster: 10.25

> Or a combination of strpos and substr.


Using `strpos()`+ `substr()` instead of `strtok()` to process the ~4k file of commas is, took on average ~3x as long as using `strtok()`. I implemented a class for this and tried to optimize it by using only string positions and not copying the string repeatedly. It also took about 1/2 hour to get the code working vs. about 15 seconds to get the code working with `strtok()`; which will most programmers prefer?

https://onlinephp.io/c/2a09f <https://onlinephp.io/c/2a09f>

Size of file:           3972
Number of commas:       359
Time for strtok:        0.0027 seconds
Time for strpos/substr: 0.0089 seconds
Times `strtok()` faster:  3.31


> There are *plenty* of solutions to the specific problem you pose here, and thus many different solutions more or less appropriate.

Yes, and in all cases the existing solutions are significantly slower, except one.

And that one solution that is not significantly slower is to not deprecate `strtok()`.  Not to mention not deprecating would keep from causing lots of BC breakage.

-Mike

---

## #123931 — Markus Podar — 2024-06-27T07:44:55+00:00

URL: https://externals.io/message/123816#123931 · In reply to: #123854

Hi,


> > […]
> 
> Ah, *the dangers of providing a specific example of a broader use-case* 
> is that someone will invariably discredit the specific example instead 
> of focusing on the applicability for the broader use-case. 🤦‍♂️
> 
> To wit, here are seven (7) use-cases for which `pathinfo()` is not a 
> viable alternative:
> 
>     https://3v4l.org/RDYFs#v8.3.8 <https://3v4l.org/RDYFs#v8.3.8>
> 
> Note those seven use-cases are found in around the first 25 results when 
> searching GitHub for "strtok(".  I could probably find more if I kept 
> looking:
> 
>     https://github.com/search?q=strtok%28+language%3APHP+&type=code
>     <https://github.com/search?q=strtok%28+language%3APHP+&type=code>
> 
> 
> Regarding explode($delimiter, $str)[0] — unless it is to be 
> special-cased during compilation —it is a really inefficient way to find 
> the substring up to the first character, especially for large strings 
> and/or when in a tight loop where the explode is contained in a called 
> function.
> 
> Here is a benchmark (https://onlinephp.io/c/87341 
> <https://onlinephp.io/c/87341>) showing that — on average of the runs I 
> performed — for using `strtok()` to fully process through a 3972 byte 
> file with 359 commas it took right at */90 times/* longer using 
> explode($delimiter, $str)[0] vs. strtok($str,$delimiter). Imagine is the 
> file were 39,720 bytes, or larger, instead.
> 
>     Size of file:                3972
>     Number of commas:            359
>     Time taken for strtok:       0.0034 seconds
>     Time taken for explode:      0.3036 seconds
>     *Times `strtok()` faster:     89.1*
> 
> 
> Yes the above processes the entire file using `explode()`[0] each time 
> rather than first using explode(",") once — because of the equivalent of 
> the N+1 problem[1] where the `explode()` is buried in a function. This 
> illustrates why `strtok()` is so good for its primary use-case of parsing 
> text files. `strtok()` is fast and does not use heaps of memory on every 
> token.
> 
> This leads me to think `strtok()` */should not/* be deprecated given how 
> inefficient string handling in PHP can otherwise be, at least not 
> without a much more efficient object for string parsing.

I'm with Mike on `strtok()` and don't understand why it would be on a
deprecation list.

I see nothing inherently "wrong" or "dangerous" with it: it's one of the
"works an intended" and if you know how to use it, it works perfectly
they way it is designed.

The variations of suggestions in other replies how to handle certain use
cases of `strtok()` already shows there's no clear migration path and
depends on the situation, which is the worst.

Compare this with suggestion like `sha1()` or similar, where the
deprecation is about "the function, but not the functionality", because
SHA1 is available by other means.
But there's no clear alternative to `strtok()`, as it is its own kind.

👎 on deprecating it; if a gotcha with it is not clear (e.g. using it in
different scopes, as this was brought up), I see this rather as a
"documentation problem".

cheers,
- Markus

---

Next page: https://externals.io/message/123816.md?page=2
