Php method rewriting: Declarationofshouldbecompatiblewiththat_PHP tutorial-php Tutorial

Source: Internet
Author: User
Tags autoload
Rewrite the php method: Declarationofshouldbecompatiblewiththat. Php method rewriting: Declarationofshouldbecompatiblewiththat if you encounter different php method rewriting parameters, the error is: Declarationofshouldbecompatiblewiththat this issue is not rewritten by the php method: Declaration of shocould be compatible with that

If you encounter a php method rewrite with different parameters, the following error occurs: Declaration of shocould be compatible with that.

After searching online, I found that many posts are basically the same. this is because after php5.3, the inherited class must be defined after the parent class. if the parent class is defined first, this error will not occur after the inherited class is present. Especially http://bugs.php.net/bug.php? Id = 46851 the above also provides positive and negative examples:

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 ;}}

$ Cc = new cc ();
?>

Most of the errors are discussed because _ autoload () is used to automatically include classes. as a result, the definition of the base class is followed by that of the subclass.

I have read my code. although autoload is also used, it is an explicit pilot of several base classes. This does not happen, if you try the preceding positive and negative examples, the E_STRICT warning will appear.

Let's look at the example.

The code is as follows:
Abstract class {
// The method has no parameters.
Public static function foo () {echo 'bar ';}
}

Abstract class B extends {
// The method has parameters.
Public static function foo ($ str) {echo $ str ;}
}
?>

Lightning

The code above: The foo method in Class A has no parameters. Class B adds parameters when inheriting A and rewriting the foo method. Therefore, A warning similar to the following E_STRICT level is generated:

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

The code is as follows:
Abstract class {
// The method has no parameters.
Public static function foo () {echo 'bar ';}
}

Abstract class B extends {
// The method has parameters.
Public static function foo ($ str = NULL) {echo $ str ;}
}
?>

When class B overrides the foo method, it just needs to specify a default value for the newly added parameter.

Real reason:

In fact, if the parameters of the subclass rewrite method and the base class are different, you only need to give the parameter a default value so that the compiler considers the parameter can be null, so that the rewrite method and the function signature of the base class method can be the same.

Those who often use JAVA will surely know that in JAVA or C ++, the signature of the function of the rewrite method should be consistent with that of the basic class function. I think this is also in line with the natural law, because override is the meaning of Overwrite. since it is covered, it should be consistent with the original function. Otherwise, how can it be "covered ~ In addition, method rewriting is mostly used to rewrite virtual functions or, more clearly, the functions that rewrite interfaces. if the function signatures are inconsistent during rewriting, what else should I do with interfaces...

Therefore, in the new PHP version, I think it is very useful to define the warning error of this E_STRICT. I should remind the programmer of the correct rewrite method.

In the end, I still despise the posts copied and copied above. if the sequence of the definition of the base class and subclass in a language cannot be disrupted, it indicates that the compiler is very problematic. it is obviously a bug.

Should of shocould be compatible with that if you encounter different php method rewriting parameters, the error is: Declaration of shocould be compatible with that...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.