This article mainly introduces the namespace in PHP and explains the namespace) for more information, see
Overview
PHP's support for namespaces has gone through a difficult journey. Fortunately, PHP introduced namespaces from 5.3. Since PHP
Actually tangled for a long time to do not use this thing, but look at the latest frame laravel,cakephp and so on the new version are used, so still follow the way to learn from the crowd.namespace and C + + concepts are similar, but the usage is a
Copy CodeThe code is as follows:
namespace Bak\ba\ba;
Function Bab () {
echo "BI";
}
namespace KKK;
function K1 () {
echo "K1";
}
\bak\ba\ba\bab ();//Use a different namespace under the KKK namespace, note the previous \
Use \bak\ba\ba as b;//is
: syntax error, unexpected 'empty' (T_EMPTY), expecting identifier (T_STRING) or '(' in namespace.php on line 4
如上,使用某些内置函数名(如empty,isset,print)会报syntax error,而某些内置函数名却没有问题(如addcslashes,mysql_select_db,explode)
请问这是什么原因?又该该如何规避?希望大神能解答,谢谢
Reply
Test
namespace will load automatically, but the two files I have tested are not loaded automatically:
#/DB/MySql.class.phpnamespace DB;class MySql{ public function __construct() { var_dump(__FILE__); }}
#/index.phpnamespace Home;use
For example, 2 classes of files
a.phpnamespace A;class Test1{}b.phpnamespace B;class Test2{}
So how do you write it in c.php? is to write
include('a.php');$c = new \A\Test1()
or a direct
use A;$c = new \A\Test1()
Reply content:
For example, 2
Just a little bit of code.
Assign (' name ', ' Ned '); $smarty->debugging = true; $smarty->display (' Index.tpl ');? > Code as above, very simple, two pieces of code of the file structure in the same directory In this way,
Create c.php file under/test folder, content
namespace Test\driver;
Class C
{
function test ()
{
echo "C";
}
}
?>
/Under Build test.php file, content
Untitled Document
Include "test/c.php";
Use Test\driver;
$c =new C
Hello, I used in the program WebSocket function, the program debugging no error, but to the service error, found on the internet, said PHP version of a http://s.yanghao.org/program/viewdetail.php?i=140457
The PHP version of my machine is 5.4, the
Namespace Library mode for a class file
The class is introduced in the following way, with the error class not Found:
namespace Frontend\block\customer\account;use yii;use frontend\block\blockbase;//use yii\base\model;//use backend\
Address: http://www.sitepoint.com/php-namespaces-import-alias-resolution/
The first part of this series discusses why PHP namespaces are useful and the namespace keywords. This article will continue to study how to use commands and PHP resolve
Namespaces can be used starting from php5.3, and namespaces are used to prevent duplicate elements of namespaces. The elements of a namespace include: class, function, const ( Starting from php5.3 You can also use const to define constants outside
For namespaces, the official documentation has been described in detail [view], and I've done a bit of practice and summary here.Namespace one of the most explicit purposes is to solve the problem of duplicate names, PHP does not allow two functions
2, using namespaces to solve what problem?
. Resolve name conflicts, such as defining a class, which is exactly the same as the class in PHP or a class library included in.
. To improve code readability, namespaces have an alias feature that helps
Namespace one of the most explicit purpose is to solve the problem of duplicate name, PHP does not allow two functions or classes appear the same names, otherwise a fatal error will occur. In this case, just avoid naming duplicates to resolve
For
1, what is a namespace?A namespace is a special scope that contains identifiers that are located under that scope, and which is itself an identifier. You can match the namespace to the directory of the operating system. A namespace is the equivalent
One of the most important new features of PHP 5.3 released recently is the addition of namespaces. This article introduces some terms, parsing rules, and application of some advanced functions of PHP namespaces, hoping to help readers use namespaces
PHP Advanced Programming Learning Note 2014.06.12
Namespaces Overview
PHP supports namespaces at the beginning of the 5.3.0 version. What is a namespace? In a broad sense, namespaces are a way of encapsulating things. This abstract concept can be
[Namespace] namespace is the basis for using PHP namespaces.
Bytes --------------------------------------------------------------------------------------------------
I. PHP namespace mainly solves three types of conflicts: constants, functions, and
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.