How to Overwrite a Function in PHP

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();

How to override built-in PHP function(s)?

I think it could be done like so:

//First rename existing function
rename_function('strlen', 'new_strlen');
//Override function with another
override_function('strlen', '$string', 'return override_strlen($string);');

//Create the other function
function override_strlen($string){
return new_strlen($string);
}

found it here

Notice that every host must have http://php.net/manual/en/book.apd.php installed on the server.

Edit

Another way is to use namespaces

<?php
namespace mysql2pdo;
use PDO;
function mysql_connect() {
return new PDO();
}
echo mysql_connect(); // Causes error because we don't have the parameters
?>

Test it here

PHP Override function inside class from outside

PHP doesn't support run-time function swizzling like the Swift language, for instance.

What you should try to do is instead create a "WPSEO_Frontend_Base" class and then create another "WP_SEO_Frontend" child class which extends from it overrides the head() function.

Here's some sample code:

wpseo.frontend.base.php

class WPSEO_Frontend_Base {
public function head() {
echo '<!-- / ', $this->head_product_name(), ". -->\n\n";
return;
}
}

wpseo.frontend.php

require 'wpseo.frontend.base.php';

class WPSEO_Frontend extends WPSEO_Frontend_Base {
function head(){
// Override head() here
}
}

PHP override function of a single instance

In order to fully understand what you are trying to achieve here, your desired PHP version should be known first, PHP 7 is more ideal for OOP approaches than any previous version.

If the binding of your anonymous function is the problem, you can bind the scope of a function as of PHP >= 5.4 to an instance, e.g.

$a->testFunc = Closure::bind(function() {
// here the object scope was gone...
$this->var = "overridden";
}, $a);

As of PHP >= 7 you can call bindTo immediately on the created Closure

$a->testFunc = (function() {
// here the object scope was gone...
$this->var = "overridden";
})->bindTo($a);

Though your approach of what you are trying to achieve is beyond my imagination. Maybe you should try to clarify your goal and I'll workout all possible solutions.

Overwriting a PHP variable in a function and use it in another file

You can add your error, code and/or message as a query string for the redirect.

<?php
$qs = http_build_query(
[
'error' => true,
'errorMsg' => 'Email already exists'
]
);
var_dump($qs);

Output:

string(37) "error=1&errorMsg=Email+already+exists"

Attach the query string to your redirect location.

Example of use of a code parameter for a path like: error.php?code=23. On error.php:

<?php
$error_codes = [
'23' => 'Email already exists.'
];

if(isset($_GET['code']) && isset($error_codes[$_GET['code']])){
echo $error_codes[$_GET['code']];
}

override a function wrapped with if (!function_exists)

If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.

The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.

The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way

PHP Overriding methods rules

The rule is that a method signature must be compatible with the method it overrides. Let's look at the two methods in your hierarchy:

protected function playing($game = 'ball');

public function playing($gameType = 'chess', $location = 'backyard');

The changes:

  1. Visibility: protected -> public; increasing the visibility is compatible (the opposite will cause errors).

  2. Arguments: no change (same number of required arguments and maximum number of arguments)



Related Topics



Leave a reply



Submit