Home > Backend Development > PHP Tutorial > oop - php中为什么在private方法里重新定义与父类同名的私有函数会报警告呢?

oop - php中为什么在private方法里重新定义与父类同名的私有函数会报警告呢?

WBOY
Release: 2016-06-06 20:51:32
Original
1189 people have browsed it

首先声明,这是一个STRICT错误,如果你不在error_reporting里把它打开,一般是看不到的,以下是我的代码

<?php class A
{
    private function test($a, $b, $c)
    {
        echo 'test';
    }
}

class B extends A
{
    private function test($d)
    {
        echo 'hello';
    }
}

new B();
Copy after login
Copy after login

它会报错

Strict Standards: Declaration of B::test() should be compatible with that of A::test() in /home/work/test.php on line 17
Copy after login
Copy after login

如果是public函数重载还能说的过去,而我只是在私有函数中使用了同名的方法,但是对于子类来说这并不算是重载吧,为什么这样还会报警告呢?

回复内容:

首先声明,这是一个STRICT错误,如果你不在error_reporting里把它打开,一般是看不到的,以下是我的代码

<?php class A
{
    private function test($a, $b, $c)
    {
        echo 'test';
    }
}

class B extends A
{
    private function test($d)
    {
        echo 'hello';
    }
}

new B();
Copy after login
Copy after login

它会报错

Strict Standards: Declaration of B::test() should be compatible with that of A::test() in /home/work/test.php on line 17
Copy after login
Copy after login

如果是public函数重载还能说的过去,而我只是在私有函数中使用了同名的方法,但是对于子类来说这并不算是重载吧,为什么这样还会报警告呢?

贴个链接:http://stackoverflow.com/questions/31...

在种情况下,STRICT模式要求:
1.access level 相同
2.参数 相同
3.默认参数值 相同

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