<?php class C1 { public function say() { return 'hello'; } }
<?php require 'C1.php'; require 'C1.php';會有Cannot redeclare class的錯誤。
<?php include 'C1.php'; include 'C1.php';會有Cannot redeclare class的錯誤。
<?php require_once 'C1.php'; require_once 'C1.php';或者:
<?php include_once 'C1.php'; include_once 'C1.php';則不會報錯。
<?php class C1 { public function say() { return 'hello'; } }修改test.php爲如下內容:
<?php include_once 'C1.php'; include_once 'C2.php';會有Cannot redeclare class的錯誤。
<?php namespace foo; class C1 { public function say() { return 'hello'; } } ?>
<?php namespace bar; class C1 { public function say() { return 'hello world'; } }
<?php require_once './C1.php'; require_once './C2.php'; $c1 = new \foo\C1(); echo $c1->say(); $c2 = new \bar\C1(); echo $c2->say();
運行結果以下: php
$ /usr/bin/php C1_C2_test.php hellohello world
http://my.oschina.net/u/867608/blog/127351
http://my.oschina.net/Jacker/blog/32943 shell