Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

PHP declaration of class must be compatible with abstract class

So we're running software (not written by us) on our server and we started getting errors emailed to us that look like this:

Could not instantiate the 'multilingual' module because of the following error. Stack traces are unfortunately not available for this type of error:

Error Message:

Declaration of CMHMultilingualMultilingual::getData($project_id, $record) must be compatible with ExternalModulesAbstractExternalModule::getData($projectId, $recordId, $eventId = '', $format = 'array') File: modules/multilingual_v1.9.8/Multilingual.php Line: 486

However, we have another server that runs it just fine. So as to not have to delve into the code for this software, I've created a simple PHP program that does the same thing: fails on one server, works on the other.

class AbstractClass {
    public function DoThisThing($var1, $var2, $var3 = "")
    {
        echo "DO THIS THING!";
    }
}

class ConcreteClass extends AbstractClass {
    public function DoThisThing($var1, $var2) {
        echo "DID THIS THING!";
    }
}

$thisThing = new ConcreteClass();
$thisThing->DoThisThing(1, 2);

The server that this fails on runs PHP 7.4.14 and the server it works on is 7.3.20. From what I can tell, since $var3 is optional in AbstractClass, it should be fine right? Or do the signatures have to be exact? Is this something introduced in PHP 7.4 or is there a setting I can change?

question from:https://stackoverflow.com/questions/65924908/php-declaration-of-class-must-be-compatible-with-abstract-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With PHP 7.4, you do have to match the arguments. This would be the correct way to write this code:

<?php

class AbstractClass {
    public function DoThisThing($var1, $var2, $var3 = "")
    {
        echo "DO THIS THING!";
    }
}

class ConcreteClass extends AbstractClass {
    public function DoThisThing($var1, $var2, $var3 = "") {
        echo "DID THIS THING!";
    }
}

$thisThing = new ConcreteClass();
$thisThing->DoThisThing(1, 2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...