如何文档化你的PHP类(二)
如何文档化你的PHP类(二)
[br]作者:stefano Locati 翻译:limodou
文档化函数或方法
成员函数或方法使用@function标记被文档化。
--------------------------------------------------------------------------------
/*! @function getItemingroup
@abstract gets a bagitem of a given group and a given position
@param groupno int - the delivery group ordinal position in the bag
@param pos int - the position of the bagitem within the group
@result Object - the BagItem in a given position of given group
or -1 if it could not be found
*/
--------------------------------------------------------------------------------
文档化一个方法。
@function标记声明了一个函数并且后面跟着函数或成员函数名。然后你可以象前面一样使用 @abstract和@discussion标记。然而还有两个额外的标记。@param标记用于描述函数的参数;第一个词假设为变量的名字,其它的则为任意的文本描述。我建议要声明想要的变量类型,尽管PHP不是一个强类型语言。 @result标记被用于描述返回值。
文档化变量
变量或类变量都使用@var标记来描述。在这个标记中,第一个词被认为是变量的名字,同时其它的则为任意的文本描述。象前面一样,我建议写出所期望的变量类型是好的做法。它也是一个文档化所有类变量的好主意。
文档化一个类变量。
--------------------------------------------------------------------------------
/*! @var idsession string - an unique session identifier */
var $idsession;
--------------------------------------------------------------------------------
最后接触
--------------------------------------------------------------------------------
/*! @header myprojectname
@abstract a virtual store to shop on mars
@discussion The difference [...]
*/
--------------------------------------------------------------------------------
@header标记用来提供一些关于被文档化的项目或类组的一般性信息。@header标记本身跟着项目的名字 ,而且可以用@abstract标记和@discussion标记来补充说明。因为类通常存在于不同的文件中(一个文件一个类,且用类的名字给文件名字是一种好的想法),你可能想知道应该将@header 标记放在什么地方。答案很让人吃惊,哪都可以。我的建议是:如果它比较长就把它放在一个独立的文件中,或如果是一个简短的说明就把它放在最重要的类的前面。
如何修改脚本用于PHP
从Apple得到的初始的HeaderDoc脚本是用于C或C++头文件的,所以要用在PHP中需要对它做一些小改动 。如果你对细节没有兴趣,你可以从这里下 载,并且跳过下面部分。
修改源程序所做的唯一的事情就是在主perl文件中,使脚本可以接受.php和.php3后缀。
--------------------------------------------------------------------------------
$ diff headerDoc2HTML.pl /usr/local/bin/headerdoc2html
195c195
---
> ($rootFileName = $filename) =~ s/.(h|i|php|php3)$//;
--------------------------------------------------------------------------------
运行脚本
在安装完脚本之后,假设你的类放在classes子目录下,并且你想将生成的文档放在docs目录下,你应该执行这个命令:
headerdoc2html -o docs classes/*.php
不幸的是如果存在多个PHP文件,这个脚本有一个坏习惯就是将那些文件分割到不同的目录中去,使得在类的文档中浏览变得很困难。而且因为初始的脚本是为C/C++头文件所写的(头文件中只有类和函数的声明而没有他们的定义),脚本会将函数名下的所有代码输出,直到碰到";",所以典型的就是代码的第一行。
但是在你好不容易读到现在却感到绝望之前,放松,因为我写了一段简单的脚本来解决这两个问题。
--------------------------------------------------------------------------------
cat classes/*.php | sed 's/ *{/;#{/g' | tr "#" "
" > docs/all.php
headerdoc2html -o docs docs/all.php
rm docs/all.php
--------------------------------------------------------------------------------
如果你想知道为什么我在这里使用tr命令而不是都用sed来做,原因就是用在仍然用在RedHat 6.2上的sed 3.02版本不处理换行符。应该替换成新的版本sed 3.02a。如果你对sed感兴趣,可以看SED FAQ。
祝你的文档化工作好运!
翻译后话:
由于这篇文章是在Linux环境下使用的,所以在windows下的使用可能会有问题。我会试一试,能想办法就想了,实在想不出来也没有办法了。
原作者:limodou
来源:PHPX

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
