Error in Interface Program(PHP)

this program is not run succssfully.

<?php interface Mail { public function sendMail(); }; class abj implements Mail { function sendMail($a) { return $a; } } $mail=new obj(); echo $mail->sendMail(1); ?>

Error:
PHP Fatal error: Declaration of abj::sendMail() must be compatible with that of Mail::sendMail() in /var/www/html/database.php on line 8

The declaration of sendMail is different, if you want to pass a variable to its construct the variable must be set in the interface as well.

[php]<?php
interface Mail
{
public function sendMail($from, $to, $message);

}

class abj implements Mail
{

function sendMail($from, $to, $message)
{
    return $from;
    
}

}

$mail=new abj();
echo $mail->sendMail(1, 2, 3);[/php]

How can i return more than one values using function ?

add them to an array or an object and return that

This is array but returns only one value

<?php interface Mail { public function sendMail($from, $to, $message); } class abj implements Mail { function sendMail($from, $to, $message) { $arr=array($from,$to,$from); for($c=0;$c<=2;$c++) { return $arr[$c] ; } } } $mail=new abj(); echo $mail->sendMail(1, 2, 3); ?>

that is because you return $arr[$c], which translates to $arr[0], which again is the $from field

What do you intend to do with your loop?

Sponsor our Newsletter | Privacy Policy | Terms of Service