Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<refentry id="{@id}">
2
 <refnamediv>
3
  <refname>phpDocumentor Quickstart</refname>
4
  <refpurpose>phpDocumentor for newbies</refpurpose>
5
 </refnamediv>
6
 <refsynopsisdiv>
7
  <author>
8
   Gregory Beaver
9
   <authorblurb>
10
    {@link mailto:cellog@php.net cellog@php.net}
11
   </authorblurb>
12
  </author>
13
 </refsynopsisdiv>
14
 {@toc}
15
 <refsect1 id="{@id intro}">
16
  <title>What is phpDocumentor?  What can it do?</title>
17
  <para>
18
   phpDocumentor is a tool written in PHP designed to create complete documentation
19
   directly from both PHP code and external documentation.  The truth is, PHP source code
20
   is so lucid, it can practically serve as its own documentation.  phpDocumentor taps
21
   into this fact by parsing out all kinds of logical structures already found in PHP,
22
   such as files, classes, functions, define constants, global variables, and class
23
   variables/methods and organizes them into a traditional manual format.  In addition, new
24
   with version 1.3.0, source code elements introduced in PHP 5 (class constants,
25
   interfaces, and others) can also be parsed, if phpDocumentor is run through PHP 5.
26
   Output can be created for remote web
27
   browsing, print, and integration into IDE help systems through
28
   converters for HTML, PDF, and CHM (windows help files).
29
  </para>
30
  <para>
31
   phpDocumentor generates manual-format documentation by reading it from special
32
   PHP comments called {@tutorial phpDocumentor.howto.pkg#basics.docblock}.
33
   DocBlocks are where you as the author of a software project should document helpful
34
   information that can be used by others (or your future self) to
35
   determine how to use and extend your PHP package.
36
  </para>
37
  <para>
38
   Although the ability to add succinct documentation in the source code is essential,
39
   it cannot replace the importance of verbose documentation that is not
40
   in the source code, such as a user manual, or tutorials such as the one you
41
   are reading now.  If the text you see here were placed in the source code
42
   for phpDocumentor, it would serve little useful purpose, clutter up the code,
43
   and be difficult to locate.  However, the ability to hyperlink between documentation
44
   in the source code and external documentation is essential.  External documentation
45
   for function foo must be able to reference the generated in-code documentation, and
46
   with phpDocumentor this is finally possible.  To learn more, read about
47
   {@tutorial tutorials.pkg}.
48
  </para>
49
 </refsect1>
50
 <refsect1 id="{@id installation}">
51
  <title>Installation</title>
52
  <para>
53
   There are two official installation methods for phpDocumentor.  The first is through
54
   downloading and extracting one of the available archives downloadable through pear.php.net
55
   and sourceforge.net, and the other is through the PEAR installer.  There is planning for
56
   a {@link http://www.phing.org Phing} task and for distribution through other
57
   new and promising installation frameworks like the {@link http://www.zzoss.com ZZ/OSS installer}.
58
   However, only the two official installation methods are supported by phpDocumentor's developers.
59
  </para>
60
  <refsect2 id="{@id sf}">
61
   <title>Download from Pear.Php.net or Sourceforge.net</title>
62
   <para>
63
    To install phpDocumentor from a .zip or tarball downloaded directly from pear.php.net or
64
    sourceforge.net, first determine whether you will be using phpDocumentor's web or command-line
65
    interface (see the {@tutorial phpDocumentor.quickstart.pkg#basicusage} section for
66
    help in making this decision).
67
   </para>
68
   <para>
69
    If you wish to use the command-line interface, unzip the archive into any directory,
70
    say <screen>/home/myuser/phpdoc</screen> or <screen>C:\Program Files\phpdoc</screen>,
71
    and add that directory to your path statement.  To use, run the &quot;phpdoc&quot;
72
    command.  In windows, you will need to edit the phpdoc.bat file, and change the
73
    first line to the path of the CLI version of PHP (usually C:\php4\cli\php.exe by
74
    default).
75
   </para>
76
   <para>
77
    To use the web interface, you must have a web server such as Apache installed, and
78
    must have a working PHP sapi for that webserver.  To test, save the code below as
79
    phpinfo.php in your document root and browse to http://localhost/phpinfo.php
80
   </para>
81
   <para>
82
    phpinfo.php:
83
    <programlisting role="php">
84
    <![CDATA[
85
<?php
86
phpinfo();
87
?>
88
    ]]>
89
    </programlisting>
90
   </para>
91
   <para>
92
    If you see a beautiful purple display of PHP information, then your PHP setup is
93
    working.  To use phpDocumentor's web interface, simply unzip the archive into
94
    a subdirectory of your document root (such as phpdoc) and browse to that location
95
    (http://localhost/phpdoc).
96
   </para>
97
   <caution>
98
    A Javascript-enabled browser such as Netscape, Mozilla, Internet Explorer, Opera, or Konqueror is
99
    required to view the newer web interface.  If you wish to use a non-javascript browser such as
100
    links/lynx, use the old web interface, phpdoc.php at http://localhost/phpdoc/phpdoc.php.
101
   </caution>
102
  </refsect2>
103
  <refsect2 id="{@id pear}">
104
   <title>Installation through {@link http://pear.php.net PEAR}</title>
105
   <para>
106
    To install phpDocumentor through PEAR, you must first have a working installation of
107
    PEAR.  Instructions for properly installing PEAR are located at the official
108
    PEAR website, {@link http://pear.php.net}.  phpDocumentor developers do not support
109
    installation issues with PEAR, instead seek help from PEAR developers.
110
   </para>
111
   <para>
112
    Installing phpDocumentor for use on the command-line is simple.  Simply run:
113
    <screen>
114
     <![CDATA[
115
$ pear install PhpDocumentor
116
    ]]>
117
    </screen>
118
    and you then have full access
119
    to the phpdoc command, both in windows and unix, without further configuration.
120
   </para>
121
   <para>
122
    To install phpDocumentor to use the web interface, you must first change one of
123
    PEAR's configuration variables, data_dir, to be a sub-directory of your web
124
    server's document root.  The simplest way to do this is through PEAR's command-line
125
    interface with the command:
126
    <screen>
127
    <![CDATA[
128
$ pear config-set data_dir /path/to/document_root/pear
129
    ]]>
130
    </screen>
131
   </para>
132
   <para>
133
    Configuring this value through the web interface is also simple.  Click on the
134
    configuration icon in the left-hand frame, and type in the path in the data_dir
135
    text box.
136
   </para>
137
   <para>
138
    Once this configuration is complete, simply install phpDocumentor as described
139
    in the second paragraph above, and you can then browse to
140
    http://localhost/pear/PhpDocumentor to have access to the web interface.  Once
141
    this configuration step has been taken, there is never any need to change, and you
142
    can easily upgrade to future phpDocumentor versions by using pear's upgrade command.
143
   </para>
144
  </refsect2>
145
 </refsect1>
146
 <refsect1 id="{@id coding}">
147
  <title>How to document your code for use with phpDocumentor</title>
148
  <para>
149
   Documenting your code projects is straightforward and intuitive for the most part.
150
   Before you begin, you may wish to download the sample documentation project and
151
   try running phpDocumentor on them as you read along, found at
152
   {@tutorial sample1.pkg}, {@tutorial sample2.pkg}, {@tutorial sample3.pkg} or found
153
   bundled in the tutorials/ directory of your phpDocumentor distribution.  Before parsing,
154
   copy the examples to another directory.
155
  </para>
156
  <para>
157
   First of all, run phpDocumentor on the blank file sample1.php.  If you are using the command-line interface, run this command from the tutorials/sample directory:
158
  </para>
159
  <para>
160
   <screen>
161
    <![CDATA[
162
$ phpdoc -o HTML:frames:earthli -f sample1.php -t docs
163
    ]]>
164
   </screen>
165
  </para>
166
  <para>
167
   If you are using the web interface, click the &quot;Files&quot; tab, and type in
168
   the full path to <filename>tutorials/sample/sample1.php</filename>.  Then click the &quot;Output&quot;
169
   tab and type in the full path to <filename>tutorials/sample/docs</filename>, and finally click the
170
   &quot;Create&quot; button in the lower right-hand corner.
171
  </para>
172
  <para>
173
   With a web browser, open the newly created <filename>tutorials/sample/docs/index.html</filename> file
174
   and you will see the rich array of information that phpDocumentor can parse from
175
   the source code even without any documentation comments.  Inheritance information,
176
   polymorphism and constants are all recognized automatically.  Note that the only
177
   element that is not automatically documented is the global variable - to do this,
178
   you must use a phpDocumentor DocBlock as described in the next section of this
179
   quickstart manual.  Also note that although the include_once specifies a file
180
   within the include_path, phpDocumentor will not automatically parse sample2.php,
181
   you must manually specify the files or directories to parse.
182
  </para>
183
  <para>
184
   If you're feeling adventurous, experiment with the parse options available and
185
   parse the sample files a few times to see how they affect the documentation output.
186
   To find out options in the command-line interface, type
187
   <screen>
188
   <![CDATA[
189
$ phpdoc -h
190
   ]]>
191
   </screen>
192
  </para>
193
  <refsect2 id="{@id phpcomments}">
194
   <title>Documenting your PHP source code with comments</title>
195
   <para>
196
    Now open <filename>{@tutorial sample2.pkg sample2.php}</filename>.  This is the same code content as
197
    sample1.php, but it contains a full array of phpDocumentor DocBlock comments.
198
    Note that every DocBlock comment is a C-style comment with two leading asterisks
199
    (*), like so:
200
   </para>
201
   <para>
202
    <programlisting role="php">
203
/**
204
 *
205
 */
206
    </programlisting>
207
   </para>
208
   <para>
209
    All other comments are ignored by the documentation parser.  Note that although most
210
    of the documentation is plain English, there are a few &quot;@&quot; characters
211
    floating around the source code.  This symbol is used to pass a special command to
212
    the parser, and is called a tag.  If the symbol is at the beginning of a line, it
213
    is a standard tag, and if it is enclosed in {curly brackets}, it is an inline tag.
214
    You can read more about tags at {@tutorial tags.pkg} and {@tutorial inlinetags.pkg}.
215
   </para>
216
   <para>
217
    <programlisting role="php">
218
/**
219
 * {@inlinetag}
220
 * this is @not a standardtag - must begin a line.
221
 * this is a valid {@inlinetag} also
222
 * @standardtag
223
 */
224
    </programlisting>
225
   </para>
226
   <refsect3 id="{@id global}">
227
    <title>Documenting global variables</title>
228
    <para>
229
     Notice this code from sample2.php:
230
    </para>
231
    <para>
232
     <programlisting role="php">
233
/**
234
 * Special global variable declaration DocBlock
235
 * @global integer $GLOBALS['_myvar']
236
 * @name $_myvar
237
 */
238
$GLOBALS['_myvar'] = 6;
239
     </programlisting>
240
    </para>
241
    <para>
242
     In this segment, we can see the two important tags used for documenting global
243
     variables.  The {@tutorial tags.global.pkg} tag is used to tell the parser how to
244
     find a global variable declaration.  Without this tag, no documentation would
245
     be generated for $_myvar.  The @global tag can take many forms - be sure to specify
246
     the type and global name, with no description, or the parser will generate a warning
247
     and fail to document the variable.
248
    </para>
249
    <para>
250
     Now, parse <filename>{@tutorial sample3.pkg sample3.php}</filename> and observe the generated documentation.
251
     The {@tutorial tags.name.pkg} tag is used to tell the parser how the global
252
     variable would be referenced by a global statement in a function.
253
    </para>
254
    <para>
255
     <programlisting role="php">
256
     <![CDATA[
257
/**
258
 * @global integer this is a function-based @global tag,
259
 *         because the variable name is missing
260
 */
261
function someFunction()
262
{
263
    global $_myvar;
264
}
265
     ]]>
266
     </programlisting>
267
    </para>
268
    <para>
269
     The @global tag here will associate the information with the declared global statements in the
270
     function body in the same order of their declaration.
271
    </para>
272
   </refsect3>
273
   <refsect3 id="{@id warnings}">
274
    <title>Page-level DocBlock warnings</title>
275
    <para>
276
     The single most commonly asked question about using phpDocumentor involves
277
     warnings about Page-level DocBlocks.  This section will answer any
278
     questions about this warning once and for all.
279
    </para>
280
    <para>
281
     phpDocumentor organizes procedural elements and classes into special groupings called
282
     {@tutorial phpDocumentor.howto.pkg#documenting.elements.packages packages}.
283
     In earlier versions of phpDocumentor, if package was not specified explicitly using
284
     the {@tutorial tags.package.pkg} tag, the program would make an educated guess
285
     as to which package a source element belongs to.
286
    </para>
287
    <para>
288
     Over time, it became apparent that in many cases, source elements were incorrectly
289
     grouped into a package due to the guesswork phpDocumentor uses.  Finally, the decision
290
     was made to require an explicit @package tag, and to raise a warning any time this
291
     tag was missing from a top-level source element.
292
    </para>
293
    <para>
294
     The greatest confusion comes from the documenting of files.  phpDocumentor documents all
295
     source elements by reading the DocBlock that immediately precedes the element, like so:
296
    </para>
297
    <para>
298
     <programlisting role="php">
299
     <![CDATA[
300
<?php
301
/**
302
 * Documents the class following
303
 * @package SomePackage
304
 */
305
class SomeClass {
306
}
307
?>
308
     ]]>
309
     </programlisting>
310
    </para>
311
    <para>
312
     phpDocumentor also can document the contents of a file.  But how can a DocBlock immediately precede
313
     the file that contains it?  The easy answer is to assume the first DocBlock in a file documents the
314
     file that contains it, and this works well, but can be deceptive:
315
    </para>
316
    <para>
317
     <programlisting role="php">
318
     <![CDATA[
319
<?php
320
/**
321
 * Documents the class following or the file?
322
 * @package SomePackage
323
 */
324
class SomeClass {
325
}
326
?>
327
     ]]>
328
     </programlisting>
329
    </para>
330
    <para>
331
     The same example shows the ambiguity - does this DocBlock document the class, or the file?  To resolve
332
     this ambiguity, phpDocumentor uses a simple algorithm to make its decision.
333
     <orderedlist>
334
      <listitem>
335
       <simpara>
336
        If the first DocBlock in a file contains a @package tag, it documents the file unless it
337
        precedes a class declaration
338
       </simpara>
339
      </listitem>
340
      <listitem>
341
       <simpara>
342
        If the first DocBlock in a file precedes another DocBlock, it documents the file
343
       </simpara>
344
      </listitem>
345
     </orderedlist>
346
    </para>
347
    <para>
348
     <programlisting role="php">
349
     <![CDATA[
350
<?php
351
/**
352
 * This is a file-level DocBlock
353
 *
354
 * A warning will be raised, saying that to document the define, use
355
 * another DocBlock
356
 * @package SomePackage
357
 */
358
define('foo', 'bar');
359
?>
360
     ]]>
361
     </programlisting>
362
    </para>
363
    <para>
364
     <programlisting role="php">
365
     <![CDATA[
366
<?php
367
/**
368
 * This is a not a file-level DocBlock
369
 *
370
 * A warning will be raised, saying that no file-level DocBlock is present
371
 * and this DocBlock will attach to the define statement
372
 */
373
define('foo', 'bar');
374
?>
375
     ]]>
376
     </programlisting>
377
    </para>
378
    <para>
379
     <programlisting role="php">
380
     <![CDATA[
381
<?php
382
/**
383
 * This is a not a file-level DocBlock
384
 *
385
 * A warning will be raised, saying that no file-level DocBlock is present
386
 * and this DocBlock will attach to the class statement
387
 */
388
class foo {}
389
?>
390
     ]]>
391
     </programlisting>
392
    </para>
393
    <para>
394
     <programlisting role="php">
395
     <![CDATA[
396
<?php
397
/**
398
 * This is a not a file-level DocBlock, because it precedes a class declaration
399
 *
400
 * A warning will be raised, saying that no file-level DocBlock is present
401
 * and this DocBlock will attach to the class statement
402
 * @package SomePackage
403
 */
404
class foo {}
405
?>
406
     ]]>
407
     </programlisting>
408
    </para>
409
    <para>
410
     <programlisting role="php">
411
     <![CDATA[
412
<?php
413
/**
414
 * This is a file-level DocBlock
415
 *
416
 * A warning will be raised saying that the package was assumed to be
417
 * SomePackage
418
 */
419
/**
420
 * This is a normal class-level DocBlock
421
 *
422
 * this DocBlock will attach to the class statement
423
 * @package SomePackage
424
 */
425
class foo {}
426
?>
427
     ]]>
428
     </programlisting>
429
    </para>
430
    <para>
431
     <programlisting role="php">
432
     <![CDATA[
433
<?php
434
/**
435
 * This is a file-level DocBlock
436
 *
437
 * A warning will be raised saying that the package was assumed to be
438
 * SomePackage because no @package tag was used
439
 */
440
/**
441
 * This is a not a file-level DocBlock, because it precedes a class declaration
442
 *
443
 * A warning will be raised, saying that no file-level DocBlock is present
444
 * and this DocBlock will attach to the class statement
445
 * @package SomePackage
446
 */
447
class foo {}
448
?>
449
     ]]>
450
     </programlisting>
451
    </para>
452
    <para>
453
     <programlisting role="php">
454
     <![CDATA[
455
<?php
456
/**
457
 * This is a file-level DocBlock
458
 *
459
 * No warning will be raised.  This is the recommended usage
460
 * @package SomePackage
461
 */
462
/**
463
 * This is a not a file-level DocBlock, because it precedes a class declaration
464
 *
465
 * This is also the recommended usage
466
 * @package SomePackage
467
 */
468
class foo {}
469
?>
470
     ]]>
471
     </programlisting>
472
    </para>
473
   </refsect3>
474
  </refsect2>
475
  <refsect2 id="{@id tutorials}">
476
   <title>Writing external documentation and linking to source code documentation</title>
477
   <para>
478
    Although documentation parsed directly from source code is tremendously useful, it cannot stand on its own.
479
    In addition, truly useful in-code documentation must be succinct enough so that the code is not completely
480
    obscured by the documentation.  External documentation is a must for a complete documentation solution.
481
    However, external documentation must be able to link to API source documentation to be useful.  With a
482
    constantly changing API documentation, it is very easy for external documentation to become out of date.
483
    In addition, external documentation must be in a format that can be converted into other formats such as
484
    HTML, PDF and XML.
485
   </para>
486
   <para>
487
    phpDocumentor provides a simple and elegant solution to all of these problems.  External
488
    documentation in DocBook format can be easily converted to other formats.  Using inline
489
    tags, phpDocumentor can generate a consistent manual in many different formats by combining
490
    the output from parsing the source and parsing external documentation.  The words you read at this
491
    moment are in a DocBook-based file located in tutorials/phpDocumentor/phpDocumentor.quickstart.pkg
492
   </para>
493
   <para>
494
    <programlisting role="tutorial">
495
     <![CDATA[
496
<refentry id="{@}id}">
497
 <refnamediv>
498
  <refname>Simple Tutorial</refname>
499
  <refpurpose>The simplest Tutorial Possible</refpurpose>
500
 </refnamediv>
501
 <refsynopsisdiv>
502
  <author>
503
   Gregory Beaver
504
   <authorblurb>
505
    {@}link mailto:cellog@php.net cellog@php.net}
506
   </authorblurb>
507
  </author>
508
 </refsynopsisdiv>
509
 <refsect1 id="{@}id intro}">
510
  <para>Hello World</para>
511
 </refsect1>
512
</refentry>
513
     ]]>
514
    </programlisting>
515
   </para>
516
  </refsect2>
517
 </refsect1>
518
 <refsect1 id="{@id basicusage}">
519
  <title>Basic usage of phpDocumentor tool</title>
520
  <para>
521
   Now that phpDocumentor is installed, how can you use it to document a project?  First,
522
   you must understand how phpDocumentor divides your code into packages and subpackages.
523
   If you haven't already, now would be a good time to read the
524
   {@tutorial phpDocumentor.quickstart.pkg#coding} section of this quickstart.
525
  </para>
526
  <refsect2 id="{@id commandline}">
527
   <title>Command-line tool, <filename>phpdoc</filename></title>
528
  </refsect2>
529
  <refsect2 id="{@id webinterface}">
530
   <title>docbuilder, the phpDocumentor web interface</title>
531
  </refsect2>
532
 </refsect1>
533
</refentry>