Jump to content

error_reporting: hide the warnings


rick645
Go to solution Solved by Barand,

Recommended Posts

$ php -v
PHP 8.1.2-1ubuntu2.10 (cli) (built: Jan 16 2023 15:19:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.10, Copyright (c), by Zend Technologies
$ cat entry-point.php 
<?php

error_reporting(E_ALL & ~E_WARNING & ~E_USER_WARNING);

class C {
	final private function m() {}
}
$ php entry-point.php 
PHP Warning:  Private methods cannot be final as they are never overridden by other classes in /tmp//entry-point.php on line 6

Ok, the appropriate WARNING appears.
However, since there is error_reporting(...) instruction, it should not appear.
Why do you appear?

Link to comment
Share on other sites

error_reporting() won't help you if there is an error detected while parsing the file that the code is in. In this case, since there is no other file where you could put the function call, set the error reporting you want with a CLI option.

You know what's even better though? Fixing the problem the warning is telling you about.

Link to comment
Share on other sites

$ cat entry-point.php 
<?php
var_dump(ini_get('error_reporting'));
class C {
	final private function m() {}
}
$ cat php.ini 
error_reporting = E_ALL & ~E_WARNING & ~E_USER_WARNING
$ php entry-point.php 
PHP Warning:  Private methods cannot be final as they are never overridden by other classes in /tmp//entry-point.php on line 4
string(5) "22527"


Comprehensible!!!

$ php -c php.ini entry-point.php

Warning: Private methods cannot be final as they are never overridden by other classes in /tmp//entry-point.php on line 4
string(5) "32253"

Now I do not understand...

Link to comment
Share on other sites

Your problem is this line: 

9 minutes ago, rick645 said:
final private function m() {}

A private function is accessible only within the class in which it is defined (your class, "C"). 

A final function is one that is known to subclasses (of "C"), but those subclasses are not permitted to override that function. 

Since a private function is not known to subclasses, it cannot be meaningfully marked as final, hence the Warning. 

 

Either remove the final modifier or change the "private" modifier to "protected". 
Protected functions are known to subclasses and, by default, can be overridden by those subclasses. 

 

Neither private nor protected functions are available to any other class. 
See also Visibility in the Documentation. 

 

Regards, 
   Phill  W.

 

Link to comment
Share on other sites

  • Solution

The point is that that your code to set the error reporting level is not being executed.

When you run a PHP script is is first parsed for errors (startup errors). If none are found the script is then executed (and your error reporting code will be executed) but, as in this case, if errors are found your code is not executed.

This was pointed out to you by requinix earlier...

1 hour ago, requinix said:

error_reporting() won't help you if there is an error detected while parsing the file that the code is in.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.