Phpnamespace and use problems/create a C. php file in the test folder, content
Namespace Test \ Driver;
Class C
{
Function test ()
{
Echo "c ";
}
}
?>
/.
Untitled Document
Include "test/C. php ";
Use Test \ Driver;
$ C = new C ();
$ C-> test ();
Echo"
";
?>
Running error:
(! ) SCREAM: Error suppression ignored
(! ) Fatal error: Class 'C' not found in E: \ hongyunlai \ thinkphp32 \ test. php on line 14
Call Stack
# Time Memory Function Location
1 0.0006 247520 {main} () .. \ test. php: 0
Reply to discussion (solution)
If the test. php file
Include "test/C. php ";
$ C = new Test \ Driver \ C ();
$ C-> test ();
Echo"
";
In this case, it can be displayed normally.
C
Content
include "test/C.php"; use Test\Driver as p;$c=new p\C();$c->test();
Well, you can write it like this. But must this be written?
Sorry, isn't use referencing the current namespace?
This can also be done.
namespace Test\Driver;include "test/C.php"; $c=new C();$c->test();
Use is a reference namespace
I am not very clear about the correct usage
Does the referenced space name reference all the classes in the namespace?
Can classes in this namespace be directly used?
Now, I feel that use does not play any role.
I just supplemented it and I am selling it now
Use namespace in three ways
include "test/C.php"; $c=new Test\Driver\C();$c->test();
include "test/C.php"; use Test\Driver;$c=new Driver\C();$c->test();
include "test/C.php"; use Test\Driver as p;$c=new p();$c->test();
Does the referenced space name reference all the classes in the namespace?
As long as you include it, all the classes in it will be loaded.
What kind of selective loading does not exist? Self-Deception
I understand both the first and third types.
Include "test/C. php ";
Use Test \ Driver;
$ C = new Driver \ C (); doesn't understand what the Driver means.
$ C-> test ();
Your namespace is Test \ Driver
$ C = new Driver \ C (); just take the last section of the namespace.
Ah, I did a test.
Namespace Test \ Driver;
Use Test \ Driver;
Class C
{
Function test ()
{
Echo "c ";
}
}
?>
Test. php
Include "test/C. php ";
// Use Test \ Driver;
$ C = new Test \ Driver \ C ();
$ C-> test ();
Echo"
";
?>
In this case, an error is returned.
Include "test/C. php ";
// Use Test \ Driver;
$ C = new Driver \ C ();
$ C-> test ();
Echo"
";
Error message
(! ) SCREAM: Error suppression ignored
(! ) Fatal error: Class 'driver \ C' not found in E: \ hongyunlai \ thinkphp32 \ test. php on line 14
Call Stack
# Time Memory Function Location
1 0.0005 247472 {main} () .. \ test. php: 0