作者Silver Moon在 binarytides 上发表的一篇《 40 Techniques to enhance your php code 》,文中主要探讨了如何提高PHP代码质量,供开发者学习与参考。 1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首
作者Silver Moon在binarytides上发表的一篇《40+ Techniques to enhance your php code》,文中主要探讨了如何提高PHP代码质量,供开发者学习与参考。
1.不要使用相对路径
常常会看到:
<ol><li><span><span>require_once('../../lib/some_class.php'); </span></span></li></ol>
该方法有很多缺点:
它首先查找指定的php包含路径, 然后查找当前目录.
因此会检查过多路径.
如果该脚本被另一目录的脚本包含, 它的基本目录变成了另一脚本所在的目录.
另一问题, 当定时任务运行该脚本, 它的上级目录可能就不是工作目录了.
因此最佳选择是使用绝对路径:
<ol> <li><span><span>define('ROOT' , '/var/www/project/'); </span></span></li> <li><span>require_once(ROOT . '../../lib/some_class.php'); </span></li> <li><span> </span></li> <li><span>//rest of the code </span></li> </ol>
我们定义了一个绝对路径, 值被写死了. 我们还可以改进它. 路径 /var/www/project 也可能会改变, 那么我们每次都要改变它吗? 不是的, 我们可以使用__FILE__常量, 如:
<ol> <li><span><span>//suppose your script is /var/www/project/index.php </span></span></li> <li><span>//Then __FILE__ will always have that full path. </span></li> <li><span> </span></li> <li><span>define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME)); </span></li> <li><span>require_once(ROOT . '../../lib/some_class.php'); </span></li> <li><span> </span></li> <li><span>//rest of the code </span></li> </ol>
现在, 无论你移到哪个目录, 如移到一个外网的服务器上, 代码无须更改便可正确运行.
可以在脚本头部引入多个文件, 像类库, 工具文件和助手函数等, 如:
<ol> <li><span><span>require_once('lib/Database.php'); </span></span></li> <li><span>require_once('lib/Mail.php'); </span></li> <li><span>require_once('helpers/utitlity_functions.php'); </span></li> </ol>
这种用法相当原始. 应该更灵活点. 应编写个助手函数包含文件. 例如:
<ol> <li><span><span>function load_class($class_name) </span></span></li> <li><span>{ </span></li> <li><span> //path to the class file </span></li> <li> <span> $</span><span>path</span><span> = </span><span>ROOT</span><span> . '/lib/' . $class_name . '.php'); </span> </li> <li><span> require_once( $path ); </span></li> <li><span>} </span></li> <li><span> </span></li> <li><span>load_class('Database'); </span></li> <li><span>load_class('Mail'); </span></li> </ol>
有什么不一样吗? 该代码更具可读性.
將来你可以按需扩展该函数, 如:
<ol> <li><span><span>function load_class($class_name) </span></span></li> <li><span>{ </span></li> <li><span> //path to the class file </span></li> <li> <span> $</span><span>path</span><span> = </span><span>ROOT</span><span> . '/lib/' . $class_name . '.php'); </span> </li> <li><span> </span></li> <li><span> if(file_exists($path)) </span></li> <li><span> { </span></li> <li><span> require_once( $path ); </span></li> <li><span> } </span></li> <li><span>} </span></li> </ol>
还可做得更多:
为同样文件查找多个目录
能很容易的改变放置类文件的目录, 无须在代码各处一一修改
可使用类似的函数加载文件, 如html内容.
在开发环境中, 我们打印数据库查询语句, 转存有问题的变量值, 而一旦问题解决, 我们注释或删除它们. 然而更好的做法是保留调试代码.
在开发环境中, 你可以:
<ol> <li><span><span>define('ENVIRONMENT' , 'development'); </span></span></li> <li><span> </span></li> <li> <span>if(! $db-</span><span>></span><span>query( $query ) </span> </li> <li><span>{ </span></li> <li> <span> if(</span><span>ENVIRONMENT</span><span> == 'development') </span> </li> <li><span> { </span></li> <li><span> echo "$query failed"; </span></li> <li><span> } </span></li> <li><span> else </span></li> <li><span> { </span></li> <li><span> echo "Database error. Please contact administrator"; </span></li> <li><span> } </span></li> <li><span>} </span></li> </ol>
在服务器中, 你可以:
<ol> <li><span><span>define('ENVIRONMENT' , 'production'); </span></span></li> <li> <span>if(! $db-</span><span>></span><span>query( $query ) </span> </li> <li><span>{ </span></li> <li> <span> if(</span><span>ENVIRONMENT</span><span> == 'development') </span> </li> <li><span> { </span></li> <li><span> echo "$query failed"; </span></li> <li><span> } </span></li> <li><span> else </span></li> <li><span> { </span></li> <li><span> echo "Database error. Please contact administrator"; </span></li> <li><span> } </span></li> <li><span>} </span></li> </ol>
4. 使用可跨平台的函数执行命令
system, exec, passthru, shell_exec 这4个函数可用于执行系统命令. 每个的行为都有细微差别. 问题在于, 当在共享主机中, 某些函数可能被选择性的禁用. 大多数新手趋于每次首先检查哪个函数可用, 然而再使用它.
更好的方案是封成函数一个可跨平台的函数。
<ol> <li><span><span>01 /** </span></span></li> <li><span>02 Method to execute a command in the terminal </span></li> <li><span>03 Uses : </span></li> <li><span>04 </span></li> <li><span>05 1. system </span></li> <li><span>06 2. passthru </span></li> <li><span>07 3. exec </span></li> <li><span>08 4. shell_exec </span></li> <li><span>09 </span></li> <li><span>10 */ </span></li> <li><span>11 function terminal($command) </span></li> <li><span>12 { </span></li> <li><span>13 //system </span></li> <li><span>14 if(function_exists('system')) </span></li> <li><span>15 { </span></li> <li><span>16 ob_start(); </span></li> <li><span>17 system($command , $return_var); </span></li> <li> <span>18 $</span><span>output</span><span> = </span><span>ob_get_contents</span><span>(); </span> </li> <li><span>19 ob_end_clean(); </span></li> <li><span>20 } </span></li> <li><span>21 //passthru </span></li> <li><span>22 else if(function_exists('passthru')) </span></li> <li><span>23 { </span></li> <li><span>24 ob_start(); </span></li> <li><span>25 passthru($command , $return_var); </span></li> <li> <span>26 $</span><span>output</span><span> = </span><span>ob_get_contents</span><span>(); </span> </li> <li><span>27 ob_end_clean(); </span></li> <li><span>28 } </span></li> <li><span>29 </span></li> <li><span>30 //exec </span></li> <li><span>31 else if(function_exists('exec')) </span></li> <li><span>32 { </span></li> <li><span>33 exec($command , $output , $return_var); </span></li> <li> <span>34 $</span><span>output</span><span> = </span><span>implode</span><span>("\n" , $output); </span> </li> <li><span>35 } </span></li> <li><span>36 </span></li> <li><span>37 //shell_exec </span></li> <li><span>38 else if(function_exists('shell_exec')) </span></li> <li><span>39 { </span></li> <li> <span>40 $</span><span>output</span><span> = </span><span>shell_exec</span><span>($command) ; </span> </li> <li><span>41 } </span></li> <li><span>42 </span></li> <li><span>43 else </span></li> <li><span>44 { </span></li> <li> <span>45 $</span><span>output</span><span> = </span><span>'Command execution not possible on this system'</span><span>; </span> </li> <li> <span>46 $</span><span>return_var</span><span> = </span><span>1</span><span>; </span> </li> <li><span>47 } </span></li> <li><span>48 </span></li> <li> <span>49 return array('output' =</span><span>></span><span> $output , 'status' =</span><span>></span><span> $return_var); </span> </li> <li><span>50 } </span></li> <li><span>51 </span></li> <li><span>52 terminal('ls'); </span></li> </ol>
上面的函数將运行shell命令, 只要有一个系统函数可用, 这保持了代码的一致性.
<ol> <li><span><span>1 function add_to_cart($item_id , $qty) </span></span></li> <li><span>2 { </span></li> <li><span>3 $_SESSION['cart']['item_id'] = $qty; </span></li> <li><span>4 } </span></li> <li><span>5 </span></li> <li><span>6 add_to_cart( 'IPHONE3' , 2 ); </span></li> </ol>
使用上面的函数添加单个项目. 而当添加项列表的时候,你要创建另一个函数吗? 不用, 只要稍加留意不同类型的参数, 就会更灵活. 如:
<ol> <li><span><span>01 function add_to_cart($item_id , $qty) </span></span></li> <li><span>02 { </span></li> <li><span>03 if(!is_array($item_id)) </span></li> <li><span>04 { </span></li> <li><span>05 $_SESSION['cart']['item_id'] = $qty; </span></li> <li><span>06 } </span></li> <li><span>07 </span></li> <li><span>08 else </span></li> <li><span>09 { </span></li> <li> <span>10 foreach($item_id as $</span><span>i_id</span><span> =</span><span>></span><span> $qty) </span> </li> <li><span>11 { </span></li> <li><span>12 $_SESSION['cart']['i_id'] = $qty; </span></li> <li><span>13 } </span></li> <li><span>14 } </span></li> <li><span>15 } </span></li> <li><span>16 </span></li> <li><span>17 add_to_cart( 'IPHONE3' , 2 ); </span></li> <li> <span>18 add_to_cart( array('IPHONE3' =</span><span>></span><span> 2 , 'IPAD' =</span><span>></span><span> 5) ); </span> </li> </ol>
现在, 同个函数可以处理不同类型的输入参数了. 可以参照上面的例子重构你的多处代码, 使其更智能.
我很想知道为什么这么多关于php建议的博客文章都没提到这点.
<ol> <li><span><span>1 </span><span></span><span>php</span><span> </span></span></li> <li><span>2 </span></li> <li><span>3 echo "Hello"; </span></li> <li><span>4 </span></li> <li><span>5 //Now dont close this tag </span></li> </ol>
这將节约你很多时间. 我们举个例子:
一个 super_class.php 文件
<ol> <li><span><span>01 </span><span></span><span>php</span><span> </span></span></li> <li><span>02 class super_class </span></li> <li><span>03 { </span></li> <li><span>04 function super_function() </span></li> <li><span>05 { </span></li> <li><span>06 //super code </span></li> <li><span>07 } </span></li> <li><span>08 } </span></li> <li> <span>09 </span><span>?></span><span> </span> </li> <li><span>10 //super extra character after the closing tag </span></li> </ol>
index.php
<ol> <li><span><span>1 require_once('super_class.php'); </span></span></li> <li><span>2 </span></li> <li><span>3 //echo an image or pdf , or set the cookies or session data </span></li> </ol>
这样, 你將会得到一个 Headers already send error. 为什么? 因为 “super extra character” 已经被输出了. 现在你得开始调试啦. 这会花费大量时间寻找 super extra 的位置.
因此, 养成省略关闭符的习惯:
<ol> <li><span><span>01 </span><span></span><span>php</span><span> </span></span></li> <li><span>02 class super_class </span></li> <li><span>03 { </span></li> <li><span>04 function super_function() </span></li> <li><span>05 { </span></li> <li><span>06 //super code </span></li> <li><span>07 } </span></li> <li><span>08 } </span></li> <li><span>09 </span></li> <li><span>10 //No closing tag </span></li> </ol>
这会更好。
7. 在某地方收集所有输入, 一次输出给浏览器这称为输出缓冲, 假如说你已在不同的函数输出内容:
<ol> <li><span><span>01 function print_header() </span></span></li> <li><span>02 { </span></li> <li> <span>03 echo "</span><span><span>div</span><span> </span><span>id</span><span>=</span><span>'header'</span><span>></span><span>Site Log and Login links</span><span></span><span>div</span><span>></span><span>"; </span></span> </li> <li><span>04 } </span></li> <li><span>05 </span></li> <li><span>06 function print_footer() </span></li> <li><span>07 { </span></li> <li> <span>08 echo "</span><span><span>div</span><span> </span><span>id</span><span>=</span><span>'footer'</span><span>></span><span>Site was made by me</span><span></span><span>div</span><span>></span><span>"; </span></span> </li> <li><span>09 } </span></li> <li><span>10 </span></li> <li><span>11 print_header(); </span></li> <li> <span>12 for($</span><span>i</span><span> = </span><span>0</span><span> ; $i </span><span><span> </span><span>100</span><span>; $i++) </span></span> </li> <li><span>13 { </span></li> <li> <span>14 echo "I is : $i </span><span><span>br</span><span> </span><span>/></span><span>'; </span></span> </li> <li><span>15 } </span></li> <li><span>16 print_footer(); </span></li> </ol>
替代方案, 在某地方集中收集输出. 你可以存储在函数的局部变量中, 也可以使用ob_start和ob_end_clean. 如下:
<ol> <li><span><span>01 function print_header() </span></span></li> <li><span>02 { </span></li> <li> <span>03 $</span><span>o</span><span> = </span><span>"<div>Site Log and Login links</div>"</span><span>; </span> </li> <li><span>04 return $o; </span></li> <li><span>05 } </span></li> <li><span>06 </span></li> <li><span>07 function print_footer() </span></li> <li><span>08 { </span></li> <li> <span>09 $</span><span>o</span><span> = </span><span>"<div>Site was made by me</div>"</span><span>; </span> </li> <li><span>10 return $o; </span></li> <li><span>11 } </span></li> <li><span>12 </span></li> <li><span>13 echo print_header(); </span></li> <li> <span>14 for($</span><span>i</span><span> = </span><span>0</span><span> ; $i </span><span><span> </span><span>100</span><span>; $i++) </span></span> </li> <li><span>15 { </span></li> <li> <span>16 echo "I is : $i </span><span><span>br</span><span> </span><span>/></span><span>'; </span></span> </li> <li><span>17 } </span></li> <li><span>18 echo print_footer(); </span></li> </ol>
为什么需要输出缓冲:
>>可以在发送给浏览器前更改输出. 如 str_replaces 函数或可能是 preg_replaces 或添加些监控/调试的html内容.
>>输出给浏览器的同时又做php的处理很糟糕. 你应该看到过有些站点的侧边栏或中间出现错误信息. 知道为什么会发生吗? 因为处理和输出混合了.
8. 发送正确的mime类型头信息, 如果输出非html内容的话。
输出一些xml.
<ol> <li><span><span>1 $</span><span>xml</span><span> = </span><span>'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'</span><span>; </span></span></li> <li> <span>2 $</span><span>xml</span><span> = "</span><span><span>response</span><span>></span><span> </span></span> </li> <li> <span>3 </span><span><span>code</span><span>></span><span>0</span><span></span><span>code</span><span>></span><span> </span></span> </li> <li> <span>4 </span><span></span><span>response</span><span>></span><span>"; </span> </li> <li><span>5 </span></li> <li><span>6 //Send xml data </span></li> <li><span>7 echo $xml; </span></li> </ol>
工作得不错. 但需要一些改进.
<ol> <li><span><span>1 $</span><span>xml</span><span> = </span><span>'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'</span><span>; </span></span></li> <li> <span>2 $</span><span>xml</span><span> = "</span><span><span>response</span><span>></span><span> </span></span> </li> <li> <span>3 </span><span><span>code</span><span>></span><span>0</span><span></span><span>code</span><span>></span><span> </span></span> </li> <li> <span>4 </span><span></span><span>response</span><span>></span><span>"; </span> </li> <li><span>5 </span></li> <li><span>6 //Send xml data </span></li> <li><span>7 header("content-type: text/xml"); </span></li> <li><span>8 echo $xml; </span></li> </ol>
注意header行. 该行告知浏览器发送的是xml类型的内容. 所以浏览器能正确的处理. 很多的javascript库也依赖头信息.
类似的有 javascript , css, jpg image, png image:
JavaScript
<ol> <li><span><span>1 header("content-type: application/x-javascript"); </span></span></li> <li> <span>2 echo "var </span><span>a</span><span> = </span><span>10</span><span>"; </span> </li> </ol>
CSS
<ol> <li><span><span>1 header("content-type: text/css"); </span></span></li> <li><span>2 echo "#div id { background:#000; }"; </span></li> </ol>
9. 为mysql连接设置正确的字符编码曾经遇到过在mysql表中设置了unicode/utf-8编码, phpadmin也能正确显示, 但当你获取内容并在页面输出的时候,会出现乱码. 这里的问题出在mysql连接的字符编码.
<ol> <li><span><span>01 //Attempt to connect to database </span></span></li> <li> <span>02 $</span><span>c</span><span> = </span><span>mysqli_connect</span><span>($this-</span><span>></span><span>host , $this-</span><span>></span><span>username, $this-</span><span>></span><span>password); </span> </li> <li><span>03 </span></li> <li><span>04 //Check connection validity </span></li> <li><span>05 if (!$c) </span></li> <li><span>06 { </span></li> <li> <span>07 die ("Could not connect to the database host: </span><span><span>br</span><span> </span><span>/></span><span>". mysqli_connect_error()); </span></span> </li> <li><span>08 } </span></li> <li><span>09 </span></li> <li><span>10 //Set the character set of the connection </span></li> <li><span>11 if(!mysqli_set_charset ( $c , 'UTF8' )) </span></li> <li><span>12 { </span></li> <li><span>13 die('mysqli_set_charset() failed'); </span></li> <li><span>14 } </span></li> </ol>
一旦连接数据库, 最好设置连接的 characterset. 你的应用如果要支持多语言, 这么做是必须的.
10. 使用 htmlentities 设置正确的编码选项php5.4前, 字符的默认编码是ISO-8859-1, 不能直接输出如à a等.
<ol><li><span><span>1 $</span><span>value</span><span> = </span><span>htmlentities</span><span>($this-</span><span>></span><span>value , ENT_QUOTES , CHARSET); </span></span></li></ol>
php5.4以后, 默认编码为UTF-8, 这將解决很多问题. 但如果你的应用是多语言的, 仍然要留意编码问题,.
11. 不要在应用中使用gzip压缩输出, 让apache处理考虑过使用 ob_gzhandler 吗? 不要那样做. 毫无意义. php只应用来编写应用. 不应操心服务器和浏览器的数据传输优化问题.
使用apache的mod_gzip/mod_deflate 模块压缩内容。
12. 使用json_encode输出动态javascript内容时常会用php输出动态javascript内容:
<ol> <li><span><span>01 $</span><span>images</span><span> = </span><span>array</span><span>( </span></span></li> <li><span>02 'myself.png' , 'friends.png' , 'colleagues.png' </span></li> <li><span>03 ); </span></li> <li><span>04 </span></li> <li> <span>05 $</span><span>js_code</span><span> = </span><span>''</span><span>; </span> </li> <li><span>06 </span></li> <li><span>07 foreach($images as $image) </span></li> <li><span>08 { </span></li> <li> <span>09 $js_code </span><span>.</span><span>= </span><span>"'$image' ,"</span><span>; </span> </li> <li><span>10 } </span></li> <li><span>11 </span></li> <li> <span>12 $</span><span>js_code</span><span> = </span><span>'var images = ['</span><span> . $js_code . ']; '; </span> </li> <li><span>13 </span></li> <li><span>14 echo $js_code; </span></li> <li><span>15 </span></li> <li> <span>16 //Output is var </span><span>images</span><span> = ['myself.png' ,'friends.png' ,'colleagues.png' ,]; </span> </li> </ol>
更聪明的做法, 使用 json_encode:
<ol> <li><span><span>1 $</span><span>images</span><span> = </span><span>array</span><span>( </span></span></li> <li><span>2 'myself.png' , 'friends.png' , 'colleagues.png' </span></li> <li><span>3 ); </span></li> <li><span>4 </span></li> <li> <span>5 $</span><span>js_code</span><span> = </span><span>'var images = '</span><span> . json_encode($images); </span> </li> <li><span>6 </span></li> <li><span>7 echo $js_code; </span></li> <li><span>8 </span></li> <li> <span>9 //Output is : var </span><span>images</span><span> = ["myself.png","friends.png","colleagues.png"] </span> </li> </ol>
优雅乎?
13. 写文件前, 检查目录写权限写或保存文件前, 确保目录是可写的, 假如不可写, 输出错误信息. 这会节约你很多调试时间. linux系统中, 需要处理权限, 目录权限不当会导致很多很多的问题, 文件也有可能无法读取等等.
确保你的应用足够智能, 输出某些重要信息.
<ol> <li><span><span>1 $</span><span>contents</span><span> = </span><span>"All the content"</span><span>; </span></span></li> <li> <span>2 $</span><span>file_path</span><span> = </span><span>"/var/www/project/content.txt"</span><span>; </span> </li> <li><span>3 </span></li> <li><span>4 file_put_contents($file_path , $contents); </span></li> </ol>
这大体上正确. 但有些间接的问题. file_put_contents 可能会由于几个原因失败:
>>父目录不存在
>>目录存在, 但不可写
>>文件被写锁住?
所以写文件前做明确的检查更好.
<ol> <li><span><span>01 $</span><span>contents</span><span> = </span><span>"All the content"</span><span>; </span></span></li> <li> <span>02 $</span><span>dir</span><span> = </span><span>'/var/www/project'</span><span>; </span> </li> <li> <span>03 $</span><span>file_path</span><span> = $dir . "/content.txt"; </span> </li> <li><span>04 </span></li> <li><span>05 if(is_writable($dir)) </span></li> <li><span>06 { </span></li> <li><span>07 file_put_contents($file_path , $contents); </span></li> <li><span>08 } </span></li> <li><span>09 else </span></li> <li><span>10 { </span></li> <li><span>11 die("Directory $dir is not writable, or does not exist. Please check"); </span></li> <li><span>12 } </span></li> </ol>
这么做后, 你会得到一个文件在何处写及为什么失败的明确信息.
14. 更改应用创建的文件权限在 linux环境中, 权限问题可能会浪费你很多时间. 从今往后, 无论何时, 当你创建一些文件后, 确保使用chmod设置正确权限. 否则的话, 可能文件先是由"php"用户创建, 但你用其它的用户登录工作, 系统將会拒绝访问或打开文件, 你不得不奋力获取root权限, 更改文件的权限等等.
<ol> <li><span><span>1 // Read and write for owner, read for everybody else </span></span></li> <li><span>2 chmod("/somedir/somefile", 0644); </span></li> <li><span>3 </span></li> <li><span>4 // Everything for owner, read and execute for others </span></li> <li><span>5 chmod("/somedir/somefile", 0755); </span></li> </ol>
15. 不要依赖submit按钮值来检查表单提交行为
<ol> <li><span><span>1 if($_POST['submit'] == 'Save') </span></span></li> <li><span>2 { </span></li> <li><span>3 //Save the things </span></li> <li><span>4 } </span></li> </ol>
上面大多数情况正确, 除了应用是多语言的. 'Save' 可能代表其它含义. 你怎么区分它们呢. 因此, 不要依赖于submit按钮的值.
<ol> <li><span><span>1 if( $_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['submit']) ) </span></span></li> <li><span>2 { </span></li> <li><span>3 //Save the things </span></li> <li><span>4 } </span></li> </ol>
现在你从submit按钮值中解脱出来了.
16. 为函数内总具有相同值的变量定义成静态变量
<ol> <li><span><span>1 //Delay for some time </span></span></li> <li><span>2 function delay() </span></li> <li><span>3 { </span></li> <li> <span>4 $</span><span>sync_delay</span><span> = </span><span>get_option</span><span>('sync_delay'); </span> </li> <li><span>5 </span></li> <li> <span>6 echo "</span><span><span>br</span><span> </span><span>/></span><span>Delaying for $sync_delay seconds..."; </span></span> </li> <li><span>7 sleep($sync_delay); </span></li> <li> <span>8 echo "Done </span><span><span>br</span><span> </span><span>/></span><span>"; </span></span> </li> <li><span>9 } </span></li> </ol>
17. 不要直接使用 $_SESSION 变量
某些简单例子:
<ol> <li><span><span>1 $_SESSION['username'] = $username; </span></span></li> <li><span> </span></li> <li> <span>2 $</span><span>username</span><span> = $_SESSION['username']; </span> </li> <li><span> </span></li> </ol>
这会导致某些问题. 如果在同个域名中运行了多个应用, session 变量可能会冲突. 两个不同的应用可能使用同一个session key. 例如, 一个前端门户, 和一个后台管理系统使用同一域名.
从现在开始, 使用应用相关的key和一个包装函数:
<ol> <li><span><span>01 define('APP_ID' , 'abc_corp_ecommerce'); </span></span></li> <li><span>02 </span></li> <li><span>03 //Function to get a session variable </span></li> <li><span>04 function session_get($key) </span></li> <li><span>05 { </span></li> <li> <span>06 $</span><span>k</span><span> = </span><span>APP_ID</span><span> . '.' . $key; </span> </li> <li><span>07 </span></li> <li><span>08 if(isset($_SESSION[$k])) </span></li> <li><span>09 { </span></li> <li><span>10 return $_SESSION[$k]; </span></li> <li><span>11 } </span></li> <li><span>12 </span></li> <li><span>13 return false; </span></li> <li><span>14 } </span></li> <li><span>15 </span></li> <li><span>16 //Function set the session variable </span></li> <li><span>17 function session_set($key , $value) </span></li> <li><span>18 { </span></li> <li> <span>19 $</span><span>k</span><span> = </span><span>APP_ID</span><span> . '.' . $key; </span> </li> <li><span>20 $_SESSION[$k] = $value; </span></li> <li><span>21 </span></li> <li><span>22 return true; </span></li> <li><span>23 } </span></li> </ol>
18. 將工具函数封装到类中
假如你在某文件中定义了很多工具函数:
<ol> <li><span><span>01 function utility_a() </span></span></li> <li><span>02 { </span></li> <li><span>03 //This function does a utility thing like string processing </span></li> <li><span>04 } </span></li> <li><span>05 </span></li> <li><span>06 function utility_b() </span></li> <li><span>07 { </span></li> <li><span>08 //This function does nother utility thing like database processing </span></li> <li><span>09 } </span></li> <li><span>10 </span></li> <li><span>11 function utility_c() </span></li> <li><span>12 { </span></li> <li><span>13 //This function is ... </span></li> <li><span>14 } </span></li> </ol>
这些函数的使用分散到应用各处. 你可能想將他们封装到某个类中:
<ol> <li><span><span>01 class Utility </span></span></li> <li><span>02 { </span></li> <li><span>03 public static function utility_a() </span></li> <li><span>04 { </span></li> <li><span>05 </span></li> <li><span>06 } </span></li> <li><span>07 </span></li> <li><span>08 public static function utility_b() </span></li> <li><span>09 { </span></li> <li><span>10 </span></li> <li><span>11 } </span></li> <li><span>12 </span></li> <li><span>13 public static function utility_c() </span></li> <li><span>14 { </span></li> <li><span>15 </span></li> <li><span>16 } </span></li> <li><span>17 } </span></li> <li><span>18 </span></li> <li><span>19 //and call them as </span></li> <li><span>20 </span></li> <li> <span>21 $</span><span>a</span><span> = </span><span>Utility</span><span>::utility_a(); </span> </li> <li> <span>22 $</span><span>b</span><span> = </span><span>Utility</span><span>::utility_b(); </span> </li> </ol>
显而易见的好处是, 如果php内建有同名的函数, 这样可以避免冲突.
另一种看法是, 你可以在同个应用中为同个类维护多个版本, 而不导致冲突. 这是封装的基本好处,无它。