vineri, 28 ianuarie 2011

PHP error: Strict Standards PEAR::isError()

Last day I tried to make a script for sending mail via PHP using SMTP, the script looked something like this:


require_once "Mail.php"; 

$from = "FROMmail@mail.com"; 
$to = "TOmail@mail.com"; 
$subject = "Subject";
$body = "Body text";     

$host = "mail.mail.com"; 
$username = "FROMmail@mail.com";
$password = "mail.com_pass";
$headers = array ('From' => $from,
  'To' => $to,
   'Subject' => $subject);
$mail = new Mail();
$smtp = & $mail->factory('smtp',
array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
if(PEAR::isError($smtp->send($to,$headers,$body)))
{
$error = $mail->getMessage();
}
else 
{
$msg = 'Mail send';
} 
The code above generated a bunch of Strict Standards errors, like the one here:


Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in /usr/share/php/Net/SMTP.php


As the error states the Pear::isError is called statically but the function was not declared as so. This is probably because of a compatibility issue between PHP 4 and PHP 5, and inside PEAR scripts there are a lot of calls to this PEAR::isError() and all the calls are statical.
To solve this problem you go to /usr/share/php/ open the PEAR.php file and at line 250 you will find the function isError just add static in front of the function declaration. Try again and you should see the Strict Standards errors no more.
You may find similar errors in the Mail.php file, don't be shy, go and check the files and make the appropriate changes, that's the big benefit of Open Source.