tp5 blog project combat
Development preparation: environment wamp, windows system as an example. Look real blog, the default will set up a development environment and framework tp5 have at least some basis.
tp5 download and install
Method One: directly in the official website to download a copy to your project directory wamp.
Method two: Download and install composer, open cmd to open the root directory of your project
Replace Ali cloud mirror (download speed)
composer config -g repo.packagist composer https://packagist.phpcomposer.com
download
composer create-project topthink/think=5.0.* tp5 --prefer-dist
At this point in your root directory project appeared tp5 folder
Open the browser localhost / your site / tp5 / public
appear
Decade of sword ... then the installation is successful.
Namespaces
Php inside the same two functions can not appear the same this time you need to use this function namespace for example because two of the same name put on two different namespaces so without error. It's like the same folder where the file can not be two identical names appears, we built two sub-folder in the folder to which files are put back on.
Namespace only functions, classes, and functions and constants are not define constants defined.
const constants defined in the general class of which, it may be a namespace class outside
Introduced earlier php namespace, you can not have any characters in front of the namespace.
<?php
namespace kj1;
function getmsg(){
echo '123';
}
const mn='chenguosong';
namespace kj2;
function getmsg(){
echo 'hello';
}
const mn='chenjialei';
So how to find and use the function you want it?
\kj1\getmsg();
echo '<br/>'.\kj1\mn;
Functions can be invoked before the space, such as this
<?php
namespace kj1;
function getmsg(){
echo '123';
}
const mn='chenguosong';
\kj2\getmsg();
namespace kj2;
function getmsg(){
echo 'hello';
}
const mn='chenjialei';
But not constant
Multi-level namespace
like this
namespace beijing\haidian;
Three access ways
1. Non-limiting access mode name
It is the direct use of the current namespace
namespace kj2;
function getmsg(){
echo 'hello';
}
getmsg();
2. qualified name of the access method
beijing\haidian\getmsg();
namespace shanghai\putuoqu\beijing\haidian;
function getmsg(){
echo 'hehe';
}
3. To access the fully qualified name of fashion
\beijing\haidian\getmsg();
The introduction of mechanisms namespace
Introducing space
use beijing\haidian\tiananmen;//引入命名空间
If you want to use the space inside, use your qualified name of the access method can not be directly accessed by way of non-limiting Name
For example this
tiananmen\getmsg();//命名空间里的方法的使用
$animal=new tiananmen\animals();
echo $animal->obj;//类的方法
echo tiananmen\animals::$name;//静态常量的访问
The introduction of elements of class
use beijing\haidian\tiananmen\animals;//这样就相当于把上一个命名空间的类animals复制了一份到当前的命名空间,可以直接使用,就像使用当前命名空间的类一样。注意,拷贝了一份就得把当前同名称的类给删除。
echo animals::$name;
$sub=new animals();
echo $sub->obj;
So do not be like the previous introduction of the namespace that each use a qualified name to be used for access.
public space
Namespace into the public space
Php namespace is not defined is public spaces for example, the following two php
2.php
<?php
function getmsg(){
echo "上海普陀";
}
const NM="陈国松";
1.php
<?php
namespace beijing;
header("content-type:text/html;charset=utf-8");
class animals{
public $obj='dog';
static $name='大黄';
}
function getmsg(){
echo '北京海淀';
}
include("./2.php");//引入公共空间
getmsg();//使用这个方法打印输出的是 北京海淀 而不是 上海普陀 说明引入的公共空间对当前命名空间是没有影响的,只有你要使用的方法在当前找不到,才会去公共空间寻找
Use constants and methods of public space
include("./2.php");
echo \NM;//如果当前的空间没有这个常量而公共空间有的话也可以直接这样echo NM
\getmsg();
The introduction of public space namespaces
<?php
function getmsg(){
echo "上海普陀";
}
const NM="陈国松";
include("./1.php");
echo NM;//访问的还是当前的NM
echo \NM;//访问的是当前的NM
echo \beijing\NM;//访问命名空间里面的NM
tp5 naming use cases inside space
For example, the following controller in the application index
Namespace is a virtual file directory does not exist, but in tp5 inside with app / index / controller correspond to the actual facilitate the development of
<?php
namespace app\index\controller;
class Index
{
public function index()
{
return 'hello';
}
}
In the fetch method thinkphp / library / think / Controller
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index()
{
return $this->fetch();
}
}
Index.php so you can use the fetch method