Phpnamespace and use problems

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

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.