You are on page 1of 52

PHP

1.0.0

heiyeluren (http://blog.csdn.net/heiyeshuwu)

laruence
2010-6-13

(http://www.laruence.com/)

PHP 1.0.0

1 API .................................................................................................................................... 2
1.1 API ............................................................................................................................................... 2
1.1.1 echo (print): ......................................................................................................................................... 2
1.1.2 printf .................................................................................................................................................... 2
1.1.3 print_rvar_dump(var_export)debug_zval_dump......................................................................... 3
1.2 ........................................................................................................................... 6
1.2.1 .................................................................................................................................... 6
1.2.2 ................................................................................................................................ 8
1.2.3 ...............................................................................................................................11
1.2.4 ...........................................................................................................................................11
2 ............................................................................................................................................... 13
2.1 ............................................................................................................................................. 13
2.2 FirePHP .............................................................................................................................................. 14
2.2.1 .................................................................................................................................. 15
2.2.2 ...................................................................................................................................... 17
2.2.3 .................................................................................................................................. 18
2.2.4 ...................................................................................................................................... 18
3 IDE .................................................................................................................................................... 19
3.1 IDE .................................................................................................................................... 19
3.1.1 Vim .................................................................................................................................................... 19
3.1.2 Zend Studio ....................................................................................................................................... 19
3.1.3 Eclipse ............................................................................................................................................... 21
3.1.4 NetBeans ........................................................................................................................................... 22
3.2 IDE ...................................................................................................................................................... 23
3.2.1 Zend Studio + Zend Debugger .......................................................................................................... 23
3.2.2 Eclipse (PDT) + Xdebug ................................................................................................................... 31
3.2.3 Vim + Xdebug + DBGp..................................................................................................................... 37
4 PHP .................................................................................................................................... 39
4.1 ..................................................................................................................................... 39
4.2 Xdebug .................................................................................................................... 39
4.2.1 ...................................................................................................................................... 41
4.3 APD(Advanced PHP Debugger) .................................................................................................................. 46
4.3.1 .......................................................................................................................................... 46
4.3.2 APD ......................................................................................................................................... 46
4.4 Xhprof ...................................................................................................................... 47
4.4.1 Xhprof ............................................................................................................................. 47
5 PHP .................................................................................................................................... 51
5.1 PHPUnit ....................................................................................................................................................... 51

1 51

PHP 1.0.0

1 API
API PHP

APIecho (print)print_rvar_dump(var_export)debug_zval_dump
debug_print_backtrace(debug_backtrace)
display_errorslog_errorserror_reportingerror_log

1.1 API
1.1.1 echo (print):

1.1.2 printf
printf , PHP ,
.
:

2 51

PHP 1.0.0

, , printf
:

1.1.3 print_rvar_dump(var_export)debug_zval_dump

API

var_dump :

3 51

PHP 1.0.0

[rint_r var_dump false NULL

var_export

4 51

PHP 1.0.0

, var_export NULL[1]
debug_zval_dump var_dump refcount
php copy on write () [2]

debug_print_backtrace
:.

1.
1

var_export var_dump : http://www.laruence.com/2008/04/03/15.html

PHP : http://tech.sina.com.cn/s/2006-10-26/09151204364.shtm
PHP : http://www.laruence.com/2008/09/19/520.html

5 51

PHP 1.0.0

1.2
1.2.1
Php.ini error_reportingdisplay_errorslog_errorserror_log

Error_reporting PHP PHP


value

constant

E_ERROR

E_WARNING

E_PARSE

E_NOTICE

16

E_CORE_ERROR

32

E_CORE_WARNING

64

E_COMPILE_ERROR

128

E_COMPILE_WARNING

256

E_USER_ERROR

512

E_USER_WARNING

1024

E_USER_NOTICE

6143

E_ALL

2048

E_STRICT

4096

E_RECOVERABLE_ERROR

E_ALL E_STRICT , Notice


/

6 51

PHP 1.0.0

error_reporting = E_ALL & ~E_NOTICE

Notic
(Notice )

Notice

Notice

Warning (Warning )

(
)

Error_reporting PHP

7 51

PHP 1.0.0

display_errors PHP

display_errors Off
log_errors
Off

log_errors error_log

error_reporting PHP

1.2.2

throw catch
trigger_errorset_error_handlerset_exception_handler api
trgger_error error_reporting
E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
Fatal ErrorWarningNotice
trigger_error

8 51

PHP 1.0.0

set_error_handler trigger_error

9 51

PHP 1.0.0

Set_exception_handler set_error_handler Set_exception_handler

10 51

PHP 1.0.0

1.2.3
, , ,
. [3].
, , , [4]

1.2.4

PHP

Log
log API fopen/fwrite file_put_contents
PHP error_log
IO IO
OO PHP4 , register_shutdown_function [5].
IO

1.
3

PHP HTML:http://www.laruence.com/2009/07/27/1020.html

PHP (@):http://www.laruence.com/2010/05/28/1565.html

PHP4 :http://www.laruence.com/2008/09/04/498.html

11 51

PHP 1.0.0

10

12 51

PHP 1.0.0

PHP

fopen/fwrite file_put_contents error_log

syslogd
[6].

2
2.1

print_r var_dump API

1.
6

http://www.php.net/manual/en/book.errorfunc.php

13 51

PHP 1.0.0

2.2 FirePHP
Web Firebug cssjs
DOM A
PHP firebug FirePHP(http://www.firephp.org )
FirePHP firebug PHP PHP FirePHP
PHP Firebug + FirePHP
HTTP firephp
( firefox )
Firefox firebug firephp
FirePHP https://addons.mozilla.org/en-US/firefox/addon/6149/

14 51

PHP 1.0.0

firebug firephp

PHP FirePHPCore PHP


http://www.firephp.org/DownloadRelease/FirePHPLibrary-FirePHPCore-0.3.1

2.2.1
FirePHP

15 51

PHP 1.0.0

firefox FireBug FirePHP


HTTP X-Wf-* HTTP

firephp
firephp firebug

firebug

16 51

PHP 1.0.0

2.2.2
FirePHP

firephp

17 51

PHP 1.0.0

2.2.3

firephp

2.2.4

18 51

PHP 1.0.0

firephp http://www.firephp.org/HQ/Use.htm

3 IDE
3.1 IDE
3.1.1 Vim
Vim ,

3.1.2 Zend Studio


d Studio 5.5.0

19 51

PHP 1.0.0

Zend Studio 6.0 Eclipse IBM JBuilder


Zend Studio Eclipse IDE
Zend Studio 7.1.2

20 51

PHP 1.0.0

Zend Studio PHP IDE Eclipse


Eclipse Java Eclipse C/C++
PHP

3.1.3 Eclipse
Eclipse PHP Galileo (PDT PHP Develop Tools)

21 51

PHP 1.0.0

3.1.4 NetBeans
NetBeans Sun Java Eclipse IDE
IDE
NetBeans 6.8 for PHP

22 51

PHP 1.0.0

PHP IDE KomodoPHP DesigerPHPEdPHPEditPHP Developer


EditplusEmeditorUltraEdit32Notepad++
Zend Studio Eclipse IDE
Eclipse Netbeans

3.2 IDE
3.2.1 Zend Studio + Zend Debugger
IDE Zend Studio ZendDebugger
PHP
PHP IDE
Web IDE

ZendDebugger Zend Zend Platform

ZendDebugger Zend IDE

IDE Web URL


/

23 51

PHP 1.0.0

ZendDebugger IDE

1 IDE IDE

2 PHP IDE
PHP

Zend Studio ZendDebugger

Windows XP SP3 ()
XAMPP Web (PHP 5.2.6) ()
ZendDebugger 5.2.x_comp
XAMPPhttp://sourceforge.net/projects/xampp/
ZendDebugger: http://downloads.zend.com/pdt/server-debugger/

Xampp 1.6.8:
http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.6.8/xampplite-win32-1.6.8.zip/download
ZendDebugger 5.2.x:
http://downloads.zend.com/pdt/server-debugger/ZendDebugger-5.2.15-cygwin_nt-i386.zip

3.2.1.1 PHP
ZendDebugger ZendDebugger.dll

xampp PHP C:\xampp\php\ext


php.ini Zend

24 51

PHP 1.0.0

[zend_debugger]
zend_extension_ts="C:\xampp\php\ext\ZendDebugger.dll"
zend_debugger.allow_hosts=127.0.0.1/32,127.0.0.1/24
zend_debugger.expose_remotely=allowed_hosts
php.ini ZendExtensionManager.dll zendOptimizer
ZendDebugger

Web

phpinfo ZendDebugger

a) PHP ZendDebugger ZendDebugger nts

b) ZendExtensionManager zendOptimizer

25 51

PHP 1.0.0

dummy.php xampp Web


C:\xampp\htdocs

3.2.1.2 Zend Studio


Window Preferences PHP Debug

PHP Debugger Zend DebuggerWeb

Configure PHP Server

26 51

PHP 1.0.0

Run Run Configurations PHP Web Page index.php


Test Debugger

OK

27 51

PHP 1.0.0

ZendDebugger Xdebug Zend Studio


()

PHP ZendStudio

3.2.1.3
Web
xampp
C:\xampp\htdocs\xamppURL http://localhost/xampp/ Zend
Studio xampp index.php

28 51

PHP 1.0.0

Run Debug As PHP Web Page

29 51

PHP 1.0.0

Yes

30 51

PHP 1.0.0

3.2.2 Eclipse (PDT) + Xdebug


Eclipse (PDT) Xdebug

Zend Studio + Zend Debugger

Eclipse Web URL


Xdebug IDE

IDE IDE

PHP
IDE
PHP

Windows XP SP3 ()
XAMPP Web (PHP 5.2.6) ()
Xdebug Xampp
Eclipsehttp://www.eclipse.org/pdt/downloads/
/

31 51

PHP 1.0.0

XAMPPhttp://sourceforge.net/projects/xampp/
Xdebug: http://xdebug.org/download.php

3.2.2.1 PHP
Xampp Xdebug.dll
xampp PHP C:\xampp\php\ext
php.ini Zend
(Xampp )

[XDebug]
;; Only Zend OR (!) XDebug
zend_extension_ts="\xampp\php\ext\php_xdebug.dll"
xdebug.remote_enable=true
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.profiler_enable=1
xdebug.profiler_output_dir="\xampp\tmp"
php.ini ZendExtensionManager.dll zendOptimizer
ZendDebugger

Web

32 51

PHP 1.0.0

phpinfo Xdebug

3.2.2.2 Eclipse
Window Preferences Debug

PHP Debugger XDebug Configure XDebug

33 51

PHP 1.0.0

PHP Server

3.2.2.3
Eclipse PHP Project xampp index.php Eclipse
Run Run Configurations

34 51

PHP 1.0.0

Close Run Debug A PHP Web Page

35 51

PHP 1.0.0

Zend Studio

XDEBUG_SESSION_START=xxx
Eclipse Web Xdebug
eclipse 9000 Xdebug

36 51

PHP 1.0.0

3.2.3 Vim + Xdebug + DBGp


Linux , Vim + DBGp PHP .

:PHP 5+ with Xdebug


Vim 7.0+, +python, +signs
Python 2.0+
Vim DBGp

3.2.3.1
Xdebug, :
zend_extension=/path_to_ext_dir /xdebug.so
xdebug.remote_enable = 1
#
xdebug.remote_port = 9001
#
xdebug.remote_host = localhost #

, , , Apache ()
php_value Xdebug:
php_value xdebug.remote_port
php_value xdebug.remote_host

Vim +python, Vim big feature pythoninterp, Vim


python signs Vim :version,:

Features Included , +python +sings, Vim .


[7]:
./configure --with-features=big --enable-pythoninterp

DBGp [8], , plugin debugger.py debugger.vim $HOME/.vim


.

1.
7

, python-config, with-python-config-dir python config

37 51

PHP 1.0.0

Vim :scriptnames .
, Vim PHP , Xdebug 9001 ,
DBGp:
let g:debuggerPort = 9001

3.2.3.2
, http://abc.baidu.com/dev/1.php, Vim 1.php, :Bp
:

F5, , 5 ,
http://abc.example.com/dev/1.php?XDEBUG_SESSION_START=1 , :

, ,,.

3.2.3.3
, XDEBUG_SESSION_START ,
XDEBUG_CONFIG idekey=
.

2.
http://www.vim.org/scripts/script.php?script_id=1929

38 51

PHP 1.0.0

, export XDEBUG_CONFIG=idekey=vim.

4 PHP
4.1

microtime() time()

4.2 Xdebug

39 51

PHP 1.0.0

Xdebug

Xdebug
Zend Zend PHP
Zend [9]

handler Xdebug
cachegrind.out.xxxx
cachegrind
Windows WinCachegrind Linux KCachegrind Web Webgrind

1.
9

PHP : http://www.laruence.com/2009/06/14/945.html

40 51

PHP 1.0.0

4.2.1
Xdebug Zend Linux
php.ini
Windows XAMPP php.ini

xdebug.trace_output_dir
trace
xdebug.profiler_output_dir
xdebug.profiler_output_name
xdebug.auto_trace trace
xdebug.auto_profile

Web

phpinfo xdebug

41 51

PHP 1.0.0

42 51

PHP 1.0.0

WinCacheGrind

trace

43 51

PHP 1.0.0

Webgrind Webgrind PHP

Web config.php

Webgrind

44 51

PHP 1.0.0

XDebug : http://xdebug.org/
WinCacheGrind: http://sourceforge.net/projects/wincachegrind/
Webgrind: http://code.google.com/p/webgrind/
KCachegrind: http://kcachegrind.sourceforge.net/

45 51

PHP 1.0.0

4.3 APD(Advanced PHP Debugger)


APD Advanced PHP Debugger PHP PHP
APD

[10]

4.3.1
http://pecl.php.net/package/apd APD,
tar zxf apd-1.0.1.tgz
cd apd-1.0.1/
phpize
./configure
make
make install
PHP
zend_extension = /absolute/path/to/apd.so
apd.dumpdir = /absolute/path/to/trace/directory
apd.statement_tracing = 0
PHP zend_extension
zend_extension
zend_extension_ts
zend_extension_debug
zend_extension_debug_ts

(non ZTS, non debug build)


(ZTS, non debug build)
(non ZTS,
debug build)
(ZTS,
debug build)

4.3.2 APD
1. PHP apd_set_pprof_trace()
apd_set_pprof_trace();

1.
10

PHP

46 51

PHP 1.0.0

2.

3. apd.dumpdir/pprof_pid.ext
: CGI PHP '-e' APD php
-e -f script.php
4. pprofp
bash-2.05b$ pprofp -R /tmp/pprof.22141.0
Trace for /home/dan/testapd.php
Total Elapsed Time = 0.00
Total System Time = 0.00
Total User Time
= 0.00
Real
User
System
secs/
cumm
%Time (excl/cumm) (excl/cumm) (excl/cumm) Calls
call
s/call Memory Usage Name
-------------------------------------------------------------------------------------100.0 0.00 0.00 0.00 0.00 0.00 0.00
1 0.0000 0.0009
0 main
56.9 0.00 0.00 0.00 0.00 0.00 0.00
1 0.0005 0.0005
0 apd_set_pprof_trace
28.0 0.00 0.00 0.00 0.00 0.00 0.00
10 0.0000 0.0000
0 preg_replace
14.3 0.00 0.00 0.00 0.00 0.00 0.00
10 0.0000 0.0000
0 str_replace
5. -R "cumm call"
"s/call"
6. KCacheGrind pprof2calltree

4.4 Xhprof
Xdebug Xdebug
Xdebug CPU XhprofXhprof
Facebook

4.4.1 Xhprof
1.
2. Xhprof
Xdebug
3. Xhprof Web Xdebug

4.4.1.1
Xhprof PHP Linux
/

47 51

PHP 1.0.0

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib <path_to_htdocs>
cd extension
phpize
./configure
make
make install

Cp xhprof Web Web so

php.ini
[xhprof]
extension=xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
xhprof.output_dir=<directory_for_storing_xhprof_runs>

Web

4.4.1.2 Xhprof
Xhprof Xhprof

48 51

PHP 1.0.0

xhprof_enable() xhprofxhprof_disable()

ID

49 51

PHP 1.0.0

xhprof Web url


http:///xhprof/index.php?run= RunId&source=

Graphviz

Xhprof http://pecl.php.net/package/xhprof
Xhprof http://mirror.facebook.net/facebook/xhprof/doc.html
Graphviz : http://www.graphviz.org/

50 51

PHP 1.0.0

5 PHP

PHP SimpleTest PHPUnit SimpleTest 2008


PHP4.x PHP PHPUnit

5.1 PHPUnit
PHPUnit PHP PHP5 JUnit3 xUnit
( Kent Beck )
PHPUnit PHP
Xdebug phing
Selenium
PHPUnit
PHPUnit http://www.cublog.cn/u1/57558/showart_507369.html

51 51