How to Replace a Function in PHP (Such as Mail) and Make It Do Something Else

Is it possible to replace a function in php (such as mail) and make it do something else?

There is an extension that allows you to override functions. It is meant to be used for debugging, but I guess you can use it for your needs. Take a look:

http://www.php.net/manual/en/function.override-function.php

If you wish to call the original function within your version, make sure you read this comment:
http://no.php.net/manual/en/function.override-function.php#50821

Redefining PHP function?

Nope, that throws an error:

Fatal error: Cannot redeclare foo()

The runkit provides options, including runkit_function_rename() and runkit_function_redefine().

Is it possible to overwrite a function in PHP

Edit

To address comments that this answer doesn't directly address the
original question. If you got here from a Google Search, start here

There is a function available called override_function that actually fits the bill. However, given that this function is part of The Advanced PHP Debugger extension, it's hard to make an argument that override_function() is intended for production use. Therefore, I would say "No", it is not possible to overwrite a function with the intent that the original questioner had in mind.

Original Answer

This is where you should take advantage of OOP, specifically polymorphism.

interface Fooable
{
public function ihatefooexamples();
}

class Foo implements Fooable
{
public function ihatefooexamples()
{
return "boo-foo!";
}
}

class FooBar implements Fooable
{
public function ihatefooexamples()
{
return "really boo-foo";
}
}

$foo = new Foo();

if (10 == $_GET['foolevel']) {
$foo = new FooBar();
}

echo $foo->ihatefooexamples();

PHP mail() function replacement?

Right now Swift Mailer is one of the best mail solutions around for PHP. It's highly modular and can easily be configured to suit your needs. You could define multiple transports (in a config file for example) and use the one you need when you change providers.

Here is an example from the docs:

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself')
;

// Send the message
$result = $mailer->send($message);

Replace PHP mail function with customised version

If you have the runkit extension installed, you may be interested in using runkit_function_redefine to override the email function. Unfortunately, with PHP, native overriding of functions is not supported.

Reference: http://ca.php.net/runkit

Reference: http://ca.php.net/manual/en/function.runkit-function-redefine.php

Otherwise, you may also try and give override_function a shot.

Reference: http://php.net/manual/en/function.override-function.php

Enjoy and good luck!



Related Topics



Leave a reply



Submit