Home > Backend Development > PHP Tutorial > PHP method rewriting: Declaration of should be compatible with that_PHP tutorial

PHP method rewriting: Declaration of should be compatible with that_PHP tutorial

WBOY
Release: 2016-07-13 10:13:00
Original
1355 people have browsed it

php method rewriting: Declaration of should be compatible with that If the problem does not occur, please refer to the solution.

After searching on the Internet, I found that many posts basically copied the same thing. They said that this is because after PHP5.3 version, it is required that the inherited class must be defined after the parent class. If the parent class is defined first, the inherited class comes last. This error will not occur. In particular, http://bugs.php.net/bug.php?id=46851 also provides positive and negative examples in a serious way:

The code is as follows

// this code does trigger a strict message
error_reporting( E_ALL | E_STRICT );

class cc extends c { function test() { return null; } }
class c { function test( $a ) { return 1; } }

$cc = new cc();
?>
< ?php
// this code does NOT trigger a strict message
error_reporting( E_ALL | E_STRICT );

class c { function test( $a ) { return 1; } }
class cc extends c { function test() { return null; } }

代码如下

// this code does trigger a strict message
error_reporting( E_ALL | E_STRICT );

class cc extends c { function test() { return null; } }
class c { function test( $a ) { return 1; } }

$cc = new cc();
?>
< ?php
// this code does NOT trigger a strict message
error_reporting( E_ALL | E_STRICT );

class c { function test( $a ) { return 1; } }
class cc extends c { function test() { return null; } }

$cc = new cc();
?>

$cc = new cc();
?>

And discussed that most of the errors are caused by using _autoload() to automatically include the class, resulting in the definition of the base class at the back and the definition of the subclass at the front.

I looked at my code. Although autoload is indeed used, several base classes are explicitly imported first. There is no such situation. I also tried the positive and negative examples above. An E_STRICT warning will appear.

Look at the example again

代码如下  
abstract class A {
// 方法无参数
public static function foo(){ echo 'bar'; }
}

abstract class B extends A {
// 方法有参数
public static function foo($str){ echo $str; }
}
?>

Like lightning

As in the code above: the foo method in class A has no parameters, and class B adds parameters when overriding the foo method after inheriting A, so a warning similar to the following E_STRICT level will be generated:

Strict standards: Declaration of ... should be compatible with that of

代码如下  
abstract class A {
// 方法无参数
public static function foo(){ echo 'bar'; }
}

abstract class B extends A {
// 方法有参数
public static function foo($str = NULL){ echo $str; }
}
?>

Class B can specify a default value for the newly added parameter when overriding the foo method

The real reason:

In fact, if the parameters of the subclass overridden method are different from those of the base class, just give the parameters a default value so that the compiler thinks the parameters can be empty, and keep the function signatures of the overridden method and the base class method the same.

Students who often use JAVA must know that in JAVA or C++, the function signature of an overridden method should be consistent with the base class function. I think this is also in line with the laws of nature, because override originally means override. Well, since it is covered, it should be consistent with the original function, otherwise how can it be "covered"~ And method rewriting is mostly used to rewrite virtual functions or, more clearly, to rewrite interface functions. If overridden When the function signatures are inconsistent, why do we need an interface? . .

So in the new version of PHP, I think it is very useful to define this E_STRICT warning error to remind programmers whether their rewriting method is correct or not.

Finally, I still despise the copied posts above. If a language cannot even disrupt the order of definitions of base classes and subclasses, it means that there is something very wrong with the compiler. It is obviously a bug.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/917545.htmlTechArticlephp method rewriting: Declaration of should be compatible with that If you encounter php method rewriting, the parameters are different, Error: Declaration of should be compatible with that this kind of problem is not...
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