Diewuxi

Belive tomorrow will be better, love science and technology, support communication and understanding, always ready for thought turn.

Blog / engineering_technology / computer / code_interpreter / PHP 笔记

Blog


Article^ Parent

PHP 笔记


Date: 2018-05-15 10:00:00
Description: PHP 笔记。
Keywords: PHP, 类, 命名空间
Category: engineering_technology/computer/code_interpreter
Tag: php
Link: https://www.diewuxi.com/blog/article/66.html

PHP 笔记。

Class type

------------------------------------------------------------------- -------------------------------------------------------
                define          in class visit  out of class visit  define                  in class        out of clas
-------------   ------------    -------------   ------------------- -----------------       -------------   ---------------
need instante   $foo;           $this->foo;     $bar->foo;
                foo(){}         $this->foo();   $bar->foo();        final foo(){}           $this->foo();   $bar->foo();


------------------------------------------------------------------- -------------------------------------------------------
                define          in class visit  out of class visit  define                  in class        out of clas
-------------   ------------    -------------   ------------------- -----------------       -------------   ---------------
static type     static $foo;    self::$foo;     bar::$foo;          const foo;              self::foo;      bar::foo;
                static foo(){}  self::foo();    bar::foo()          final static foo(){}    self::foo();    bar::foo()
-------------   --------------  -------------   --------------      --------------------    --------------  ---------------
                        

Namespace

Meaning

  1. Avoid name collisions between user code, PHP internal code and third party code.
  2. Alias or shorten longname involved by above point.

Statement

Only classes(including abstracts and traits), interfaces, functions and constants are affected by namespace.

Note: Fully qualified names(i.e. names starting with a backslash) are not allowed in namespace declarations, because such constructs are interpreted as relative namespace expressions.

namespace MyProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
                        

Subnamespace

namespace MyProject\Sub\Level;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
                        

Defining multiple namespaces in the same file

example1:

namespace MyProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }

namespace AnotherProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
                        

example2:

namespace MyProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}

namespace AnotherProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
                        

example global:

namespace MyProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}

namespace { // global code
session_start();
$a = MyProject\connect();
echo MyProject\Connection::start();
}
                        

Use

A class name can be referred to in three ways:

  1. Unqualified name, or an unprefixed class name like $a = new foo(); or foo::staticmethod();.

If the current namespace is currentnamespace, this resolves to currentnamespace\foo. If the code is global, non-namespaced code, this resolves to foo.

One caveat: unqualified names for functions and constants will resolve to global functions and constants if the namespaced function or constant is not defined.

See Using namespaces: fallback to global function/constant for details.

  1. Qualified name, or a prefixed class name like $a = new subnamespace\foo(); or subnamespace\foo::staticmethod();.

If the current namespace is currentnamespace, this resolves to currentnamespace\subnamespace\foo. If the code is global, non-namespaced code, this resolves to subnamespace\foo.

  1. Fully qualified name, or a prefixed name with global prefix operator like $a = new \currentnamespace\foo(); or \currentnamespace\foo::staticmethod();.

This always resolves to the literal name specified in the code, currentnamespace\foo.

Alias and import

namespace foo;
use My\Full\Classname as Another;

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

// importing a global class
use ArrayObject;

// importing a function (PHP 5.6+)
use function My\Full\functionName;

// aliasing a function (PHP 5.6+)
use function My\Full\functionName as func;

// importing a constant (PHP 5.6+)
use const My\Full\CONSTANT;
                        

Note that for namespaced names(fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

use My\Full\Classname as Another, My\Full\NSname;

$obj = new Another; // instantiates object of class My\Full\Classname
$obj = new \Another; // instantiates object of class Another
$obj = new Another\thing; // instantiates object of class My\Full\Classname\thing
$obj = new \Another\thing; // instantiates object of class Another\thing
                        

Last modified: 2020-09-15

Comments [0]

There is no comments now.

Write comment(* is necessary, and email is not shown to public)


Diewuxi 2017--2024