For PHP file calls, we sometimes use the include, include_once, require, require_once and fall into the discretion, not only beginners so, advanced engineering is also there will be tangled time. And there is still no consensus on which one to use for the best. Each team has its own code specification for each project and has its own justification for use. The similarities and differences between the four functions are not discussed here.
Our team is advocating the use of require_once, why use require_once instead of require. Give a reason today.
The use of require can cause redeclare errors when multiple subclasses inherit the same parent class
Because require is not a method, it is a block of statements. So, he executes the referenced file again, so if you refer to two classes in a file that inherit from the same parent class, the problem is caused by this. And require_once does not, it will first determine whether it is quoted, bitter has been skipped.
Look at the code, first write an abstract parent class:
[PHP]
abstractclass.php
abstractclass.php
Then implement two subclasses:
[PHP]
requireclassa.php
Use require instead of require_once
Require ' abstractclass.php ';
Class Requireclassa extends abstractclass{
}
requireclassa.php
Use require instead of require_once
Require ' abstractclass.php ';
Class Requireclassa extends abstractclass{
}
[PHP]
requireclassb.php
Use require instead of require_once
Require ' abstractclass.php '; class Requireclassb extends abstractclass{}
requireclassb.php
Use require instead of require_once
Require ' abstractclass.php '; class Requireclassb extends abstractclass{}
Implementation invocation:
[PHP]
test.php
Require_once ' requireclassb.php ';
Require_once ' requireclassa.php ';
$A = new Requireclassa ();
$B = new REQUIRECLASSB ();
test.php
Require_once ' requireclassb.php ';
Require_once ' requireclassa.php ';
$A = new Requireclassa ();
$B = new REQUIRECLASSB ();
Run test.php Error
[Plain]
>php test.php
>php Fatal Error:cannot redeclare class AbstractClass in/home/john/workspace/php/require/abstractclass.php on line 2
>php test.php
>php Fatal Error:cannot redeclare class AbstractClass in/home/john/workspace/php/require/abstractclass.php on line 2
http://www.bkjia.com/PHPjc/477515.html www.bkjia.com true http://www.bkjia.com/PHPjc/477515.html techarticle for PHP file calls, we sometimes use the include, include_once, require, require_once and fall into the discretion, not only beginners so, advanced engineering is also there will be tangled time. ...