Phprequire's pitfalls in class inheritance

Source: Internet
Author: User
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

 

 

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.