Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overwrite a php function that has a argument type hint set?

I am trying to extend a package with my own functional ability. But the package code has type hints in the function calls, to other classes that are part of the package. I am just looking for a way to modify the code.

More details about what I'm trying to do at https://laracasts.com/discuss/channels/general-discussion/type-hint-hell

I have tried changing the code to use interfaces and abstracts, but i cant seem to prevent the "Declaration of class .... must be compatible with"error.

This is what i'm trying to do in a nutshell.

The package has this type of setup.

class ClassA {}

class ClassB {
    public function makeClassA(ClassA $classA) : ClassA
    {
        return $classA;
    }
}

This is what I am trying to do.

class ClassANew {}

class ClassC extends ClassB {
    public function makeClassA(ClassANew $classA) : ClassANew
    {
        return $classA;
    }
}

I get the following error,

"PHP Fatal error: Declaration of ClassC::makeClassA(ClassANew $classA): ClassANew must be compatible with ClassB::makeClassA(ClassA $classA): ClassA"

I know I could just fork the code and strip out the locked classA from ClassB, but I was trying not to do that.

If i was going to fork the code, I looked at how to maintain the premise of the original code. So, I tried changing the ClassA references in ClassB to a ClassAInterface, but I get the same error.

Is what I'm trying to do possible?

like image 363
Terre Porter Avatar asked Sep 13 '25 06:09

Terre Porter


1 Answers

No, this is not possible to do.

Look here, for the reasons: Why is overriding method parameters a violation of strict standards in PHP?

like image 156
Manuel Mannhardt Avatar answered Sep 15 '25 20:09

Manuel Mannhardt