The code is as follows | |||||||||||||
// this code does trigger a strict message class cc extends c { function test() { return null; } }
$cc = new cc(); class c { function test( $a ) { return 1; } }
$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
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
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.
Previous article:Quickly identify the lines of code in PHP that may cause CPU spikes_PHP Tutorial
Next article:Analysis of SimpleXML function usage in PHP, phpsimplexml_PHP tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|