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
How to Include Authorization Header in Curl Post Http Request in PHP
Exotic Names for Methods, Constants, Variables and Fields - Bug or Feature
PHP Create and Save a Txt File to Root Directory
Differencebetween Sessions and Cookies in PHP
Do SQL Connections Opened with Pdo in PHP Have to Be Closed
Replace String in Text File Using PHP
Converting a Number Base 10 to Base 62 (A-Za-Z0-9)
What Is the Equivalent of JavaScript's Encodeuricomponent in PHP
Scaling a Chat App - Short Polling VS. Long Polling (Ajax, PHP)
Laravel 4 - Logging SQL Queries
How to Re-Save the Entity as Another Row in Doctrine 2
PHP Can't Connect to MySQL with Error 13 (But Command Line Can)
What Factors Make PHP Unicode-Incompatible
Remove Diacritics from a String
Storing Arrays in the Database
Remove Item from Array If It Exists in a 'Disallowed Words' Array