For php file calls, we sometimes consider using include, include_once, require, and require_once. this is not only for beginners, but also for advanced engineering. However, there is still no consensus on which one is optimal. Each item in each team... "/> <scripttype =" text/javascript"
For php file calls, we sometimes consider using include, include_once, require, and require_once. this is not only for beginners, but also for advanced engineering. However, there is still no consensus on which one is optimal. Each project of each team has its own code specifications and reasons for use. We will not repeat the similarities and differences between the four functions here.
Our team advocates require_once. why do we use require_once instead of require. Give a reason today.
The use of require may cause redeclare errors when multiple child classes inherit the same parent class.
Because require is not a method, but a statement block. Therefore, it executes the referenced file again. Therefore, if you reference two classes that inherit the same parent class in a file, the above problems will occur. But require_once does not. it will first judge whether it has been referenced, and if it is bitter, it will be skipped.
Read the code and write an abstract parent class:
[Php]
AbstractClass. php
AbstractClass. php
Implement two sub-classes:
[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 call:
[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 ();
An error occurred while running test. php.
[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