Knowledge map advanced must read: read how large-scale map data efficient storage and retrieval>>>
The reason for this error is that the class is repeatedly defined.
The first solution:
is to rename one of the two repeatedly defined classes.The second method is:
if these classes are included/require, remove the redundant include/require, or change the include/require to include respectively_ once/require_ once。Note that the two solutions apply differently.
If two PHP files define two classes with different functions, but with the same name, the first method should be used.
If you accidentally include or require the same PHP file many times, you should use the second method.
Example:
The content of file c1.php is as follows:
<?php
class C1 {
public function say() {
return 'hello';
}
}
The content of the file test.php is as follows:
<?php
require 'C1.php';
require 'C1.php';
There will be a cannot redeclare class error.
The content of the file test.php is as follows:
<?php
include 'C1.php';
include 'C1.php';
There will be a cannot redeclare class error.
The content of the file test.php is as follows:
<?php
require_once 'C1.php';
require_once 'C1.php';
Or:
<?php
include_once 'C1.php';
include_once 'C1.php';
No error will be reported.
Now create a new file, c2.php, as follows:
<?php
class C1 {
public function say() {
return 'hello';
}
}
Modify test.php as follows:
<?php
include_once 'C1.php';
include_once 'C2.php';
There will be a cannot redeclare class error.
At this point, if you don’t want to change the class names of C1. PHP and C2. PHP, you can use the namespace.
For example, the content of c1.php is as follows:
<?php namespace foo;
class C1 {
public function say() {
return 'hello';
}
}
?>
the content of c2.php is (for a pure PHP file, the last> You may not:
<?php namespace bar;
class C1 {
public function say() {
return 'hello world';
}
}
now create the file C1_ C2_ Test.php, the content is as follows:
<?php
require_once './C1.php';
require_once './C2.php';
$c1 = new \foo\C1();
echo $c1->say();
$c2 = new \bar\C1();
echo $c2->say();
The results are as follows
$ /usr/bin/php C1_C2_test.php
hellohello world
http://my.oschina.net/u/867608/blog/127351
http://my.oschina.net/Jacker/blog/32943