Php method Rewrite: Declaration of should is compatible with that

Source: Internet
Author: User

If you encounter the PHP method rewrite, the parameter is different, the error: Declaration of should be compatible and that this problem does not go into the reference to the solution.

Search the Internet for a while, found that many posts are basically copied the same, say what this is due to the php5.3 version, the requirement that the inheriting class must be defined after the parent class, if the parent class is defined before the inheriting class, this error will not occur. In particular, http://bugs.php.net/bug.php?id=46851 above also made a positive and negative example:

The code is as follows

<?php
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 ();
?>

It is also discussed that the error situation is mostly due to the automatic include of the class with _autoload (), which causes the definition of the base class to be followed and the subclass defined in the front.

I looked at their own code, although it is also used to autoload, but are explicit pilot into a few base classes, there is no such situation, and the above positive and negative examples have tried, will appear e_strict warning.

Look at the example again

The code is as follows
<?php
Abstract class A {
Method has no parameters
public static function foo () {echo ' Bar ';}
}

Abstract class B extends A {
Method has parameters
public static function foo ($STR) {echo $str;}
}
?>

Lightning.

As the above code: the Foo method in Class A has no arguments, Class B adds parameters when it overrides the Foo method after inheriting a, and therefore produces a warning similar to the following e_strict level:

Strict standards:declaration of ... should is compatible with that

The code is as follows
<?php
Abstract class A {
Method has no parameters
public static function foo () {echo ' Bar ';}
}

Abstract class B extends A {
Method has parameters
public static function foo ($str = NULL) {echo $str;}
}
?>

Class B Specifies a default value for the newly added parameter when the Foo method is overridden

The real reason:

In fact, if the parameters of the subclass override method are not the same as the base class, just give the parameter a default value so that the compiler thinks the argument can be null, and keep the rewrite method the same as the function signature of the base class method.

Often with Java students must know, in Java or C + +, the rewrite method of the function signature should be consistent with the base class function, I think this is in accordance with the natural law, because override is the meaning of coverage, since the cover, then should be consistent with the original function, otherwise how can "cover "Live ~ and the method of rewriting more use in the rewrite virtual function or more clearly is to rewrite the interface of the function, if the rewrite when the function signature is inconsistent, but also what the interface do ...

So in a new version of PHP, I find it useful to define this e_strict warning error, to remind programmers whether their own rewriting methods are correct or not.

Finally, I would like to despise the above those copied to copy the post, if a language even the base class and subclass definition of the order can not be disrupted, that the compiler is very problematic, is obviously a bug.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.