Strict Standards: Declaration of ' ' shoul

2019-01-18 10:26发布

问题:

I just installed woocommerce 2.0 (on Wordpress) on PHP 5.4, and I got this:

Strict Standards: Declaration of WC_Gateway_BACS::process_payment() should be compatible with WC_Payment_Gateway::process_payment() in D:\my\path\to\htdocs\wordpress\plugins\woocommerce\classes\gateways\bacs\class-wc-gateway-bacs.php on line...

I check the files and found that WC_Payment_Gateway have no method process_payment(). I need to know how to resolve this (not by setting error_reporting()).

What is Strict Standards in PHP exactly?
In what condition so we get that error?

回答1:

WC_Payment_Gateway is defined in abstract-wc-payment-gateway.php and declares a method

function process_payment() {}

while WC_Gateway_BACS defines it as

function process_payment( $order_id ) { ...

(maybe you mixed up WC_Payment_Gateway and WC_Payment_Gateways).

So, different signature (0 parameters vs 1 parameter) -> strict error.
Since it seems* to be used always with one parameter you could change

function process_payment() {}

to

function process_payment($order_id) {}

(*) keep in mind I know of woocommerce only since the last five minutes, so don't take my word for it.



回答2:

Quote from PHP Manual

In PHP 5 a new error level E_STRICT is available. Prior to PHP 5.4.0 E_STRICT was not >included within E_ALL, so you would have to explicitly enable this kind of error level in >PHP < 5.4.0. Enabling E_STRICT during development has some benefits. STRICT messages >provide suggestions that can help ensure the best interoperability and forward >compatibility of your code. These messages may include things such as calling non-static >methods statically, defining properties in a compatible class definition while defined in >a used trait, and prior to PHP 5.3 some deprecated features would issue E_STRICT errors >such as assigning objects by reference upon instantiation.

You are receiving this error because WC_Gateway_BACS::process_payment() declaration is different than WC_Payment_Gateway::process_payment() (might be not the same amount of parameters etc). If WC_Payment_Gateway has no method process_payment, check it's parent class :)

Also, if you want to disable STRICT errors, add ^ E_STRICT to your error reporting configuration, for example:

error_reporting(E_ALL ^ E_STRICT);


回答3:

if you wanna keep OOP form without turning any error off, you can also:

class A
{
    public function foo() {
        ;
    }
}
class B extends A
{
    /*instead of : 
    public function foo($a, $b, $c) {*/
    public function foo() {
        list($a, $b, $c) = func_get_args();
        // ...

    }
}


回答4:

Here is a better answer - https://stackoverflow.com/a/9243127/2165415

for example,
parentClass::customMethod($thing = false) and
childClass::customMethod($thing)
so, when you call customMethod() for the class, it may trigger the error, because the child's method hasn't defined a default value for the first argument.



回答5:

When you are using the same function in a parent class and a child class, but the child class needs parameters while the parent one not, you'll get the Strict Standards error.

Example

Manager:

public function getAtPosition($position)
{
    foreach ($this->getList() as $obj)
    {
        if ($obj->getPosition() == $position)
            return $obj;
    }

    return null;
}

MenuManager extends Manager:

public function getAtPosition($position, $parent)
{
    foreach ($this->getList() as $m)
    {
        if ($m->getParent() == $parent && $m->getPosition() == $position)
            return $m;
    }

    return null;
}

This example will generate an error:

Strict standards: Declaration of MenuManager::getAtPosition() should be compatible with Manager::getAtPosition($position)

Because we don't have the same arguments to the function, so let's trick this and add arguments, even though we're not using them!

Manager:

public function getAtPosition($position, $dummy = 0) // Dummy to avoid Strict standards errors
{
    foreach ($this->getList() as $obj)
    {
        if ($obj->getPosition() == $position)
            return $obj;
    }

    return null;
}

MenuManager extends Manager:

public function getAtPosition($position, $parent = 0)
{
    foreach ($this->getList() as $m)
    {
        if ($m->getParent() == $parent && $m->getPosition() == $position)
            return $m;
    }

    return null;
}

Only one to be careful is that when using getAtPosition() from MenuManager.class.php, be sure you are actually sending 2 parameters, as we have to declare $parent = 0 in order to match the parent's declaration.

Every class extending Manager and not containing getAtPosition() will use the method from Manager.

If declared in a child class, php will use the method from the child class instead of the parent's one. There is no overloading in PHP, so that is how I worked around it until it is properly implemented.