SlideShare a Scribd company logo
1 of 18
Download to read offline
PHP 编码规范培训



           研发部总监:徐江涛

   Contact@shjuto at gmail.com
         Work @ Telligem.com
              Life @ Nanchang
主题:程序开发的编码规范

为什么需要代码规范
phpDocumentor简介
phpDoumentor tags 讲解
它山之石、可以攻玉
总结&提问
为什么需要代码规范
  初次接触者的疑虑:程序的功能,性能无疑是最重要
  的,设计也很重要,为什么却要把这么多的精力放在
  代码规范上呢,仅仅是为了方便别人阅读代码,便于
  维护吗?这样一套套的代码规范真的可以加快开发速度
  吗?
   当开发人员很好的遵守代码规范来编写代码时,才
   可以慢慢的体会到规则,才会体会到设计。可以想
   像一下,如果遵守代码规范,一个方法不得超出50
   行代码,那么大量的if else就不可能出现,取而代
   之以各种设计(如工厂,策略等设计模式)来解决
   问题

一个高手写的代码应该是容易看懂的,这也是大道至简的精髓所在
phpDocumentor简介

PHPDocumentor是一个用PHP写的工具,对于有规范注
释的php程序,它能够快速生成具有相互参照,索引等功
能的API文档。老的版本是phpdoc,从1.3.0开始,更名
为phpDocumentor,新的版本加上了对php5语法的支
持,同时,可以通过在客户端浏览器上操作生成文档,
文档可以转换为PDF,HTML,CHM几种形式,非常的方便。
PHPDocumentor工作时,会扫描指定目录下面的php源
代码,扫描其中的关键字,截取需要分析的注释,然后
分析注释中的专用的tag,生成xml文件,接着根据已经
分析完的类和模块的信息,建立相应的索引,生成xml
文件,对于生成的xml文件,使用定制的模板输出为指
定格式的文件。
phpDocumentor tags
phpDocumentor tags
@abstract
Document an abstract class, class variable or
method

/**
 * example of basic @abstract usage in a class
 * Use this if every single element of the class is abstract
 * @abstract
 */
class myabstractclass
{
   function function1($baz)
{
   ...
}
   function function2()
{
   ...
}
}
phpDocumentor tags
@access
Access control for an element. @access private prevents
documentation of the following element (if enabled).
 * This is possible, but redundant. An element has @access public by default
 * @access public
 */
class class1
{
   /**
    * all text in this DocBlock will be ignored, unless command-line switch
    * @access private
    */
   var $private_var;
   /**
    * Protected is allowed, but does absolutely nothing.
    * @access protected
    */
   /**
    * this function is documented
    */
   function publicmethod()
{
}
}
phpDocumentor tags
@author
Author of current element

 /**
  * Page-Level DocBlock example.
  * displays as Gregory Beaver<strong>cellog@php.
 net</strong>
  * , where underlined text is a "mailto:cellog@php.net" link
  * @author Gregory Beaver <cellog@php.net>
  */
 /**
  * function datafunction
  * another contributor authored this function
  * @author Joe Shmoe
  */
 function datafunction()
 {
 ...
 }
phpDocumentor tags
@category
Specify a category to organize the documented element's package
into
  /**
   * Page-Level DocBlock
   * @package MyPackage
   * @category mycategory
   */
  /**
   * @global array used for stuff
   */
  function mine()
  {
      global $baz;
      ...
  }
phpDocumentor tags
@deprecated
Document elements that have been deprecated and should not be used as
they may be removed at any time from a future version
 /**
  * @deprecated deprecated since version 2.0
  */
 function uselessfunction()
 {
 ...
 }
 /**
  * also legal
  * @deprecated
  */
 class stupidclass
 {
 ...
 }
phpDocumentor tags
 @example
 Include an external example file with syntax
 highlighting
/**
 * My function
 *
 * Here is an inline example:
 * <code>
 * <?php
 * echo strlen('6');
 * ?>
 * </code>
 * @example /path/to/example.php How to use this function
 * @example anotherexample.
inc This example is in the "examples" subdirectory
 */
function mine()
{
}
它山之石、可以攻玉
<?php
$a=100;
$b=1000;
$c=$a+$b;

<?php
$a = 100;
$b = 1000;
$c = $a + $b;
?>

运算符 、函数括号 必须空 1格
它山之石、可以攻玉
$query = “SELECT FirstName, LastName FROM employees, departments WHERE
employees.dept_id = department.dept_id AND department.Name = ‘Engineering’”;


$query = “ SELECT FirstName,LastName
FROM
employees,departments
WHERE
employees.dept_id = department.dept_id
AND
department.Name = ‘Engineering’”



SQL语句的指导写法
它山之石、可以攻玉
<?php
if(isset($name))
echo “Hello $name”;
?>

<?php
if(isset($name))
echo “Hello $name”;
$known_user = true;
?>

<?php
if(isset($name)) {
echo “Hello $name”;
}
else {
echo “Hello Stranger”;
?>
注意使用if语句
它山之石、可以攻玉
(BSD style) BSD风格
if ($condition)
{
// statement
}
(GNU style)GNU 风格
if ($condition)
{
// statement
}
(K&R style)K&R风格
if ($condition) {
// statement
}


采用 BSD 或是 GUN风格
它山之石、可以攻玉
避免使用全局变量
全局变量看起来是个非常不错的东西,但你在任何地方都可以修改它,
因此从安全和效能方面来讲你需要避免使用。
在很多情况下你需要使用全局变量;比如数据库的链接,为了避免访问
一应用程序产生多个数据库链接,可能你会选择使用全局变量
但其实在这种情况下你可以选择使用单例模式来化解这个问题。

<?php
$US_STATES = array(‘Alabama’, … , ‘Wyoming’);
?>

<?php
function us_states()
{
static $us_states = array(‘Alabama’, … , ‘Wyoming’);
return $us_states;
}
它山之石、可以攻玉
合理的命名,多个词组成的变量命名
$numElements = count( $elements );
//更好的便于理解的方式:
$num_elements = count( $elements );


避免使用短标记
<?
echo ‘helloworld’ ;
?>

<?php
echo ‘helloworld’;
?>
Thank you!

More Related Content

What's hot (9)

Js doc toolkit
Js doc toolkitJs doc toolkit
Js doc toolkit
 
Introduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDKIntroduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDK
 
NeteaseBlog Objective-C Style Guide
NeteaseBlog Objective-C Style GuideNeteaseBlog Objective-C Style Guide
NeteaseBlog Objective-C Style Guide
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹
 
Php设计模式介绍
Php设计模式介绍Php设计模式介绍
Php设计模式介绍
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
 
Javascript代码注释及文档生成
Javascript代码注释及文档生成Javascript代码注释及文档生成
Javascript代码注释及文档生成
 
Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则
 
常見設計模式介紹
常見設計模式介紹常見設計模式介紹
常見設計模式介紹
 

Viewers also liked (7)

Personal Response Systems In The Classroom
Personal Response Systems In The ClassroomPersonal Response Systems In The Classroom
Personal Response Systems In The Classroom
 
儿童市场调研报告分析
儿童市场调研报告分析儿童市场调研报告分析
儿童市场调研报告分析
 
Share xss
Share xssShare xss
Share xss
 
Ba hay PM quyết định sự thành công của dự án phần mềm
Ba hay PM  quyết định sự thành công của dự án phần mềmBa hay PM  quyết định sự thành công của dự án phần mềm
Ba hay PM quyết định sự thành công của dự án phần mềm
 
Rain catchers
Rain catchersRain catchers
Rain catchers
 
高性能No sql数据库redis
高性能No sql数据库redis高性能No sql数据库redis
高性能No sql数据库redis
 
從軟體開發角度
談 Docker 的應用
從軟體開發角度
談 Docker 的應用從軟體開發角度
談 Docker 的應用
從軟體開發角度
談 Docker 的應用
 

Similar to Php

Django development
Django developmentDjango development
Django development
loveyudu
 
I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212
Asika Simon
 
Django敏捷开发 刘天斯
Django敏捷开发 刘天斯Django敏捷开发 刘天斯
Django敏捷开发 刘天斯
liuts
 
Magento页面载入的执行流程
Magento页面载入的执行流程Magento页面载入的执行流程
Magento页面载入的执行流程
Sim Jiason
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践
yiditushe
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
hua qiu
 
HTML5概览
HTML5概览HTML5概览
HTML5概览
Adam Lu
 
Javascript autoload
Javascript autoloadJavascript autoload
Javascript autoload
jay li
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
Guo Albert
 
第三方内容开发最佳实践
第三方内容开发最佳实践第三方内容开发最佳实践
第三方内容开发最佳实践
taobao.com
 
Web development with zend framework
Web development with zend frameworkWeb development with zend framework
Web development with zend framework
thinkinlamp
 
由一个简单的程序谈起――之二
由一个简单的程序谈起――之二由一个简单的程序谈起――之二
由一个简单的程序谈起――之二
yiditushe
 

Similar to Php (20)

Django development
Django developmentDjango development
Django development
 
I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212
 
【 I Love Joomla 】- Joomla!佈景製作教學
【 I Love Joomla 】- Joomla!佈景製作教學【 I Love Joomla 】- Joomla!佈景製作教學
【 I Love Joomla 】- Joomla!佈景製作教學
 
Django敏捷开发 刘天斯
Django敏捷开发 刘天斯Django敏捷开发 刘天斯
Django敏捷开发 刘天斯
 
Magento页面载入的执行流程
Magento页面载入的执行流程Magento页面载入的执行流程
Magento页面载入的执行流程
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践
 
Js dom
Js domJs dom
Js dom
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐
 
View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎
 
View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
HTML5概览
HTML5概览HTML5概览
HTML5概览
 
Javascript autoload
Javascript autoloadJavascript autoload
Javascript autoload
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
第三方内容开发最佳实践
第三方内容开发最佳实践第三方内容开发最佳实践
第三方内容开发最佳实践
 
Web development with zend framework
Web development with zend frameworkWeb development with zend framework
Web development with zend framework
 
由一个简单的程序谈起――之二
由一个简单的程序谈起――之二由一个简单的程序谈起――之二
由一个简单的程序谈起――之二
 
jQuery介绍@disandu.com
jQuery介绍@disandu.comjQuery介绍@disandu.com
jQuery介绍@disandu.com
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
 

Php