Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.1.4 compatibility (deprecated) #508

Open
clarkk opened this issue Apr 10, 2022 · 11 comments
Open

PHP 8.1.4 compatibility (deprecated) #508

clarkk opened this issue Apr 10, 2022 · 11 comments

Comments

@clarkk
Copy link

clarkk commented Apr 10, 2022

preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated .../tcpdf/tcpdf.php(7636)

Have just downloaded latest master and get this warning

@clarkk clarkk changed the title PHP8.1.4 compatability (deprecated) PHP 8.1.4 compatability (deprecated) Apr 10, 2022
@clarkk clarkk changed the title PHP 8.1.4 compatability (deprecated) PHP 8.1.4 compatibility (deprecated) Apr 10, 2022
@williamdes
Copy link
Contributor

Hi @clarkk
Can you post the code that generates this warning?

@clarkk
Copy link
Author

clarkk commented Apr 12, 2022

You should just try to install PHP8.1.4

The log will be full of deprecated Passing null to parameter in a lot of different parts of the code

@williamdes
Copy link
Contributor

Hi @clarkk
Please provide the code that is your implementation of the library so I can add this to our test suite
This will help a lot

@clarkk
Copy link
Author

clarkk commented Apr 12, 2022

I get that, but in this case there are so many errors!

The log size is several Mb after only a short time. Most of them because null is sent as parameter which now is deprecated

@williamdes
Copy link
Contributor

Hi @clarkk
I do not want your logs ;)
I want the lines that are tcpdf calls you make, let's consider this file as an example: https://github.com/tecnickcom/TCPDF/blob/main/examples/example_066.php

Can you build some similar file with just the necessary calls so we can reproduce it and not just make obscur patches not understanding how users use this library ?

@markusramsak
Copy link
Contributor

@clarkk you can solve your issues by replacing passing NULL by an empty string.
You called the method Output() which requires you to pass a string as first param. You passed NULL which lead to your posted deprecation warning.
I use PHP 8.1 too and I get with this advice no deprecation warnings. With other function calls you could have the exact same issue and solution.

@nicklapp
Copy link

I receive the same deprecation notices on the following:

[25-May-2022 02:00:37 America/Los_Angeles] PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 6369
[25-May-2022 07:50:01 America/Los_Angeles] PHP Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 5154

I know these could be avoided by passing an empty string from the source instead of NULL, however, updating tcpdf.php to properly handle being passed NULL would make TCPDF more resilient and would resolve the deprecation notices with the least amount of code correction.

@williamdes
Copy link
Contributor

I receive the same deprecation notices on the following:

[25-May-2022 02:00:37 America/Los_Angeles] PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 6369 [25-May-2022 07:50:01 America/Los_Angeles] PHP Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 5154

I know these could be avoided by passing an empty string from the source instead of NULL, however, updating tcpdf.php to properly handle being passed NULL would make TCPDF more resilient and would resolve the deprecation notices with the least amount of code correction.

Can you mention what code lines you are talking about or send a PR ?

@nicklapp
Copy link

nicklapp commented May 25, 2022

I'm sorry, I'm not really set up for PRs. But here are the lines I'm talking about:

File: /tcpdf/tcpdf.php

Line: 5154

Current: $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
Proposed: $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', (isset($txt) ? $txt : ''));

Line: 6369

Current: if (strlen($txt) == 0) {
Proposed: if (!isset($txt)) {

Line: 7636

Current: $name = preg_replace('/[\s]+/', '_', $name);
Proposed: $name = preg_replace('/[\s]+/', '_', (isset($name) ? $name : ''));

@WanWizard
Copy link

In case of the first two, you'll still have a deprecated error if $txt has a value but is not a string.

Better:

$txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', (string) $txt);

and

if (strlen((string) $txt) == 0) {

As for line 7636: if $name is NULL, the call to the method will fail anyway, you can't generate output without a filename. It is better to bail out on an invalid filename.

@ajtsch
Copy link

ajtsch commented Apr 27, 2023

Hello,

I have corrected myself the TCPDF code but I agree it would be nice to have a fix for all these PHP > 8.1 depreciation errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants