Home Backend Development PHP Tutorial PHP design pattern one: namespace, automatic loading class, PSR-0 coding specification

PHP design pattern one: namespace, automatic loading class, PSR-0 coding specification

Apr 09, 2018 pm 04:11 PM
php load Design Patterns

本篇文章介绍的内容是php设计模式一之命名空间、自动加载类、PSR-0编码规范 ,现在分享给大家,有需要的朋友可以参考一下

一、命名空间:解决在生产环境中多人协同开发时出现类名或函数名冲突的问题;
test1.php

1

2

3

4

5

6

<?php

namespace Test1;

function test(){

    echo "Test1命名空间下的test()方法";

}

?>

Copy after login

test2.php

1

2

3

4

5

6

<?php

namespace Test2;

function test(){

    echo "Test2命名空间下的test()方法";

}

?>

Copy after login

test.php

1

2

3

4

5

6

<?php

require_once('test1.php');

require_once('test2.php');

Test1\test();    //调用Test1命名空间下的test()方法

Test2\test();

?>

Copy after login

二、自动加载类:解决在项目中引入过多的依赖类文件问题;
demo1.php

1

2

3

4

5

6

7

<?php

class Demo1{

    static function test(){

        echo "Demo1类中的test()静态方法";

    }

}

?>

Copy after login

demo2.php

1

2

3

4

5

6

7

<?php

class Demo2{

    static function test(){

        echo "Demo2类中的test()静态方法";

    }

}

?>

Copy after login

demo.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?php

//php5.3+版本,spl_autoload_register()自动注册加载类。参数为某个自定义的函数

spl_autoload_register('autoload');

//自定义一个自动加载函数,$class为类名,无须传参。spl_autoload_register()方法自动认别

function autoload($class) {

    require_once(__DIR__.'/'.$class.'.php');

}

Demo1::test();

Demo2::test();

//php5.3版本以下,使用__autoload()方法

function __autoload($class) {

    require_once(__DIR__.'/'.$class.'.php');

}

?>

Copy after login

三、PSR-0编码规范
1)、必须使用命名空间并且与文件的绝对路径一致;
2)、类名首字母须大写且与文件名保持一致;
3)、除入口文件以外其他php文件必须只有一个类且没有可执行的代码;

四、基于PSR-0编码规范编写一套基础框架
1)、目录结构

1

2

3

4

5

6

7

App            存放业务逻辑及功能实现的代码

|--Controller

    |--Home

       |--Index.php

Frame          存放与业务逻辑无关的代码,框架部分

|--Autoloader.php  自动加载类

index.php       单一入口文件

Copy after login

Autoloader.php

1

2

3

4

5

6

7

8

9

10

<?php

namespace Frame;

class Autoloader

{

    static function autoload($class)

    {

        require_once(BASEDIR.'/'.str_replace('\\','/',$class).'.php');

    }

}

?>

Copy after login

App/Controller/Home/Index.php

1

2

3

4

5

6

7

8

9

10

11

<?php

namespace App\Controller\Home;

 

class Index

{

    static function test()

    {

        echo "Home控制器下的test()静态方法";

    }

}

?>

Copy after login

index.php

1

2

3

4

5

6

7

8

<?php

define('BASEDIR',__DIR__);

require_once(BASEDIR.'/Frame/Autoloader.php');

spl_autoload_register('Frame\\Autoloader::autoload');

App\Controller\Home\Index::test();

 

//输出结果:Home控制器下的test()静态方法

?>

Copy after login

                                                  

The above is the detailed content of PHP design pattern one: namespace, automatic loading class, PSR-0 coding specification. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles