interface - php multiple interface method duplication problem

WBOY
Release: 2016-08-18 09:16:27
Original
930 people have browsed it

<code>interface A {
    public function code();
}

interface B {
    public function code(string $string);
}

class C implements A,B {
//...code
}</code>
Copy after login
Copy after login

When C needs to implement these two interfaces at the same time, is there any way to use the code methods in the two interfaces A and B at the same time? ? ? ?

Local testing can only use one of the methods

Reply content:

<code>interface A {
    public function code();
}

interface B {
    public function code(string $string);
}

class C implements A,B {
//...code
}</code>
Copy after login
Copy after login

When C needs to implement these two interfaces at the same time, is there any way to use the code methods in the two interfaces A and B at the same time? ? ? ?

Local testing can only use one of the methods

PHP cannot be overloaded (same method name, but parameters are inconsistent), but can be overriding in inheritance (same method name, regardless of whether the parameters are consistent).
In other words, no matter how the parameters of PHP change, as long as the method name is the same, it will be regarded as the same method of this class.

Of course, as a dynamic language, we can simulate Java's overloading through magic methods. Reference: http://php.net/manual/en/lang...

There is a crude method. You integrate the specific logic of the AB interface into the C interface and directly call the C interface^_^

func_get_args http://php.net/manual/en/func...

This way of writing itself is just a trial and error. Public methods are written outside and called directly in the class. Don’t write the same thing in every port, which is also very troublesome to maintain.

You can learn about the characteristics of PHP.

php does not support polymorphism, so it is best to avoid this way of writing

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!