PHP namespace namespace function call class reference detailed

Source: Internet
Author: User
Tags php class

PHP namespace namespace function call class reference detailed test analysis

In this test, it is assumed that the index.php page and the test.php page are in the same root directory, which facilitates loading of test.php pages.

The prerequisites are tested under namespace A;

Namespace A;use b\d, c\e as F;

Function call
Foo (); First try to invoke the function foo () defined in namespace "A"
\foo (); Call Global space function "Foo"
My\foo (); Call definition in Namespace "a\my" in function "foo"
F (); First attempt to invoke the function "F" defined in namespace "a"
B\foo (); Call namespace "a\b" in function "foo"
B::foo (); Call the "Foo" Method of Class "B" defined in namespace "A"
D::foo (); Use the import rule to invoke the "Foo" Method of class ' D ' defined in namespace ' B '
\b\foo (); Call the function "foo" in the namespace "B"
\b::foo (); Call the "Foo" Method of Class "B" in global space
A\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "A\a"
\a\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "a\b"


Class reference
New B (); Create an object of class ' B ' defined in namespace ' a '
New D (); Use an import rule to create an object of class ' D ' defined in namespace ' B '
New F (); Use an import rule to create an object of class ' E ' defined in the namespace ' C '
New \b (); Create an object that defines the class "B" in the global space
New \d (); Create an object that defines the class "D" in the global space
New \f (); Create an object that defines the class "F" in the global space


Function call

1 Current namespace A under function call Syntax foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); require ' test.php ';//Load define namespace ' A\my ' The page function foo () {echo ' I is the namespace "A" in the Functions foo () ';}      Function call Foo (); First try calling the function foo () defined in namespace "A"//and then try calling the global function "foo"

Result is

I am the function foo () in the namespace "A"

2 The current namespace A is lowered with the function foo () syntax My\foo () in the namespace "A\my";

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); require ' test.php ';//load the page that defines the namespace "a\my"//   Call function My\foo (); The call is defined in the namespace "a\my" in the function "foo"?>

test.php page (the page that defines the namespace "A\my")

<?php namespace A\my; function foo () {echo ' I am the namespace "A\my" in Functions foo () ';}? >

Result is

I am the function foo () in the namespace "A\my"


3. Current namespace A down the function "foo" with the namespace "a\b"

Grammar

Require ' test.php ';//The function "foo" file that loads the namespace "a\b"

B\foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//Load Namespace "A\B    "The function" foo "file B\foo (); Call namespace "a\b" in function "foo"?>

test.php page (page that defines the function "foo" for the Namespace "a\b")

<?php namespace a\b;function foo () {echo ' namespace ' a\b ' in function ' foo ';}? >

Result is

function "foo" in Namespace "a\b"


4. Current namespace A downgrade the "foo" static Method of Class "B" defined in the namespace "a"

Grammar

B::foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as f;class b{static public function fo    O () {"Foo" Method of Class "B" defined in Echo ' namespace ' A ');   }}b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "A"//If the class "a\b" is not found, try to automatically load the class "a\b"?>

Result is

"Foo" Method for Class "B" defined in namespace "A"


5. Current namespace A downgrade the "foo" static Method of Class "D" defined in this namespace "B"

Grammar

Require ' test.php '; Loading files

Use b\d; Boot class

D::foo (); Make a call

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); require ' test.php '; use b\d, c\e as F;D::foo (); Using an import rule, call the "Foo" Method of Class "D" defined in Namespace "B"///If the class "b\d" is not found, try to automatically load class "B\d"?>

test.php page (the page of the function "Foo" that defines the class "D" of the Namespace "B")

<?phpnamespace b;class d{static Public Function foo () {The "Foo" Method of Class "D" defined in Echo ' namespace "B";}}? >

Result is

The "Foo" Method for class ' D ' defined in namespace ' B '


6. The function "foo" of the Namespace "B" is reduced by the current namespace a

Grammar

Require ' test.php ';//The function "foo" file that loads the namespace "B"

\b\foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//Load Namespace "B"    The function "foo" file \b\foo (); Call namespace "B" in function "foo"?>

test.php page (page that defines the function "foo" of the Namespace "B")

<?php namespace b;function foo () {echo ' namespace ' B ' in function ' foo ';}? >

Result is

function "foo" in Namespace "B"


7. Current namespace a reduce the function "foo" static method with global space "B"

Grammar

Require ' test.php ';//The function "foo" file that loads the global space "B"

\b::foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//Load Global space "B"  The function "foo" file \b::foo (); Call the "Foo" Method of Class "B" in global space//If class "B" is not found, try to automatically load class "B"?>

test.php page (page that defines the function "foo" for Global Space "B")

<?php class b{static public function foo () {The Foo method in class B of ECHO ' Global space '; }}?>

Result is

The Foo method in Class B of global space


8. Current namespace A down with the namespace "A\a" for Class B "Foo" static method

Grammar

Require ' test.php ';//Load the namespace "A\a" for Class B "Foo" static method file

A\b::foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//Load Namespace "A\A   "Class B" foo "static method file A\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "a\a"///If the class "a\a\b" is not found, try to automatically load the class "a\a\b"?>

test.php page (the page that defines the class B "Foo" static method for the namespace "a\a")

<?php namespace A\a;class b{static public function foo () {echo ' namespace A\a ' class B Foo method '; }}?>

Result is

Class B Foo method in namespace A\a


9. Current namespace A down with the namespace "a\b" for Class B "foo" static method

Grammar

Require ' test.php ';//Load the namespace "a\b" for Class B "foo" static method file

\a\b::foo ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); require ' test.php ';//Load the namespace "a\b" for Class B "foo"  static method file \a\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "a\b"///If the class "a\b" is not found, try to automatically load the class "a\b"?>

test.php page (the page that defines the class B "foo" static method for the namespace "a\b")

<?php namespace A\b;class b{static public function foo () {echo ' namespace a\b in class B ' foo ' method '; }}?>

Result is

Foo static method for Class B in namespace a\b


Class reference

1 Current namespace A under this spatial class reference syntax $b =new B ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;class b{public $name = ' I am a namespace ' a ' An object of class "B" as defined in    $b =new B (); Create an object of class ' B ' defined in namespace ' A '//If not found, try to automatically load class "a\b" Echo $b->name;

Result is

I'm an object of class ' B ' defined in namespace ' A '


2 Class "D" References defined in namespace ' B ' under current namespace a

Grammar

Use b\d//import namespace B in class D

$b =new D ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//    Load the file class D in namespace B $d=new D (); Use an import rule to create an object of class ' D ' defined in namespace ' B '//If not found, try to automatically load class "B\d" Echo $d->name; >

test.php page (defines the page for Class D in namespace B)

<?php namespace B; Class d{Public $name = ' D ' in namespace B ';}? >

Result is

Class D in namespace B


3 class "E" References defined in namespace ' C ' under current namespace a

Grammar

Use c\e as f//import namespace C is an alias of class "E" defined in namespace "C" for Class E F

$b =new F ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//    Load the file of Class E in namespace C $f=new f (); Use an import rule to create an object of class "E" defined in the namespace "C"//If not found, try to automatically load class "C\e" Echo $f->name; >

test.php page (defines the page for Class E in namespace C)

<?php namespace C; Class e{Public $name = ' namespace C ' E ';}? >

Result is

Namespace C in Class E


4 Class "B", "D" "F" referenced in global space defined under current namespace a

Grammar

Require ' test.php ';//load global space defined in class "B", "D" "F" file

$b =new \b (); \ represents an absolute path reference to a class defined in the global space

$d =new \d ();

$f =new \f ();

index.php page

<?phpnamespace A;header ("Content-type:text/html;charset=utf-8"); use b\d, c\e as F;require ' test.php ';//   Load global space defined in class "B", "D" "F" file $b=new \b ();   Create an object that defines the class "B" in the global space//If it is not found, try to automatically load class "B" $d =new \d ();   Create an object that defines the class "D" in the global space//If it is not found, try to automatically load class "D" $f =new \f (); Create an object that defines the class "F" in the global space//If it is not found, try to automatically load class "F" echo $b->name;echo ' <br/> '; echo $d->name;echo ' <b R/> '; echo $f->name;? >

test.php page (Page of Class "B", "D" "F" defined in global space)

<?php class b{Public $name = ' global space ' B ';} Class d{Public $name = ' Global space classes D ';} Class f{Public $name = ' Global space classes F ';}? >

Result is

Class of global space Class B global space of Class D global space classes F


Appendix

<?phpnamespace A;use B\D, C\E as F; //  function Call  foo ();       //  first attempts to invoke the function foo () defined in the namespace "A"              //  then try calling global function   "foo"  \foo ();     //  Call Global space function   "foo"   my\foo ();   //  call definition in namespace "a\my" function   "foo"   f () ;        //  first attempts to invoke the function defined in the namespace "a"   "F"               //  then try calling global functions   "F"  //  class reference  new  b ();    //  Create namespaces   "A"   classes defined in   "B"   an object              //  if not found, attempt to automatically load class   "a\b"  new d ();     //  use import rules to create namespaces   classes defined in "B"     "D"   an object              //  if not found, try Auto-load class   "b\d"  new f ();    //  use import rules to create namespaces   "C"   a class defined in   "E"   an object              //  if not found, try to automatically load classes   "C\e"  new \b ();   //  create classes defined in global space   "B" An object of               //  if not found, attempts to load the class automatically   "B"  new \d ();   //  Create an object that defines the class   "D"   in the global space              //  if not found, attempt to automatically load class   "D"  new \f ();    //  create an object that defines the class   "F"   in the global space              //  if not found, attempt to automatically load class   "F"  //  invoke static method or namespace function  b\foo () in another namespace;     //  Call namespace   a\b   medium function   "foo"  b::foo ();   //  Calling namespaces   classes defined in ' A '     ' B '    " Foo "  Method             //  if class not found  " a\b "  , try auto-load class   "a\b"  d::foo ();   //  use import rules, call namespaces   "B"   classes defined in   "D"     "foo"   Method             //  if the class   "b\d"   not found, attempt to automatically load class   "b\d"  \b\foo ();   //  call namespace   functions in "B"     "foo"   \b::foo ();  //  call class in global space   "B"     "foo"   method              //  if the class   "B"   is not found, try to automatically load the class   "B"  //  static method or function in the current namespace  a\b::foo ();   //  call namespace   class defined in "A\a  "   "B"     "Foo"   Methods                  If class   "a\a\b"   not found, try to automatically load class   "a\a\b"  \a\b::foo ();  //  call namespace   "a\b" Classes defined in     "B"    "Foo"   Methods                  If the class   "a\b"   is not found, try to automatically load the class   "a\b"?>



This article is from the "11400485" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1834850

PHP namespace namespace function call class reference detailed

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.