11

After upgrading from RHEL 5x to CentOS 6x, I started seeing these errors in my httpd log:

PHP Strict Standards: Non-static method PEAR::isError() should not be called statically in /web/sites/blah/somescript.php on line 33

I saw similar errors for MDB2. More on that in a sec.

somescript.php:

32  $mdb2_dbx = MDB2::factory($dsn_mdb2, $mdb2_options);
33  if (PEAR::isError($mdb2_dbx))
34  {
35      $err = '<p>Cannot connect to database: ' . $mdb2_dbx->getMessage();
36      errorHandler($err);
37  }   

The first thing I did was edit /etc/php.ini and add & ~E_STRICT to error reporting. Restarted httpd to load the new config. Still getting these error messages.

Others mentioned the same problem with MDB2, so I updated these packages to the beta releases. This seemed to address MDB2 errors, but I'm still getting the PEAR error messages in httpd log file.

System info:

# pear list
PEAR               1.9.4   stable
MDB2               2.5.0b5 beta
MDB2_Driver_mysql  1.5.0b4 beta
MDB2_Driver_mysqli 1.5.0b4 beta

# php --version
PHP 5.4.20 (cli) (built: Sep 18 2013 19:55:33) 

# cat /etc/centos-release 
CentOS release 6.4 (Final)

# apachectl -v
Server version: Apache/2.2.15 (Unix)

Question

Is there a different way of invoking PEAR::isError() that will not produce errors?

a coder
  • 7,530
  • 20
  • 84
  • 131

3 Answers3

16

I'm afraid @johannes is incorrect - this is very doable. Simply substitute this in your recipe:

if ((new PEAR)->isError($mdb2_dbx)) {
    // Victory! Er, I mean, Error!
    ...
}
Winfield Trail
  • 5,535
  • 2
  • 27
  • 43
  • This however requires editing all libraries, which are using the "traditional" way. So yes, one could do that in one's own code, but yo still run into trouble. Better use modern libraries. – johannes Jan 22 '15 at 23:12
  • 2
    @johannes Believe it or not, some modern libraries make this basic architectural mistake - Yubico's PHP authentication library, notably. And sometimes a global find and replace beats hunting for a new solution. – Winfield Trail Jan 23 '15 at 04:26
  • Or, switching to PDO which is what I eventually did. – a coder Jan 23 '15 at 16:30
  • Some modern (or at least, recently built) libraries still do this. Look at YubiKey, for example. Knowing how to fix this problem is important. – Winfield Trail Mar 11 '15 at 18:28
  • 1
    Thank you for this answer. I agree that upgrading to a modern solution is the best way to go, but sometimes all that you need is to make a quick fix and move on. I was contacted by a client who was bothered by the log messages "PEAR::isError() should not be called statically" in a piece of code that we wrote in 2013 and haven't touched since then. I was not about to refactor that code with a modern library to "fix" an annoying warning message. Sometimes you need a quick solution. Thanks for posting! – chaimp Feb 19 '16 at 17:31
5

It may be worth noting that that calling PEAR::isError($obj) with one argument is equivalent to is_a($obj, 'PEAR_Error'), if you're updating your own code. I know it is not best practice to "unwrap" a library method like that, but it is basically just an "instance of" check.

3

No there isn't. PEAR::isError is legacy from PHP 4 times.

If changing the error level in php.ini is not enough you should check

  • whether there is another php.ini file being loaded (check phpinfo() output via Apache)
  • some script sets the error level.

If all that doesn't help, set an appropriate level using the error_level() function at runtime, or if nothing else helps, suppress the errors using the @ operator. Using @ should be avoided as it is relatively "slow" (error reporting is slow anyways ...) and it might hide other errors.

Long-term suggestion would be to use more modern libraries.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
johannes
  • 15,807
  • 3
  • 44
  • 57
  • I confirmed /etc/php.ini is what's being used (and edited). I'll double check scripts to make sure something isn't overriding php.ini – a coder Oct 08 '13 at 13:23
  • 5
    Is there a modern alternative to PEAR::isError? – a coder Oct 08 '13 at 13:47
  • 2
    Is it possible to call it non-statically? `$pear = new PEAR(); $pear->isError($something);` – Kien Nguyen Nov 12 '13 at 04:45
  • In php.ini set the error level to ignore strict rules using :: error_reporting = E_ALL & ~E_STRICT – JoshOiknine Aug 13 '14 at 03:05
  • Seriously - how do you call `PEAR::isError()` _without raising some ridiculous error_? Why aren't functions in PEAR/PearMail `static`??? – Josh M. Oct 13 '14 at 05:02
  • @JoshM. Since PEAR is historic stuff from PHP 4 times ( PHP 4 i.e. had no `static`) and they have their compatibility requirements ... as said in the answer: use some mire modern library. – johannes Oct 13 '14 at 11:40
  • Ah, okay. Yeah I switched to SwiftMailer. Thanks. – Josh M. Oct 13 '14 at 14:57
  • @JoshM. What you asked is completely possible - I see you've moved on in your own solution, but I've left an improved answer for other people with the same problem. – Winfield Trail Jan 22 '15 at 21:46