PHP require in class inheritance in the pit _php tutorial

Source: Internet
Author: User
Tags php error
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. ...

  • 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.