±¾ÎĽ«ÌÖÂÛ³£ÓõÄÁ¼ºÃµÄ´uÂeÏ°¹ß£¬»oÕß³ÆΪ´uÂe¹ae*¶£¬ÔÚPHPÁiÓo¡£
1£¬´iÎo±¨¸ae¿ªÆo
´iÎo±¨¸aeÊÇÔÚPHPÖÐÒ»¸odz£ÓÐÓõŦÄÜ£¬Ó¦Í¬Ê±ÔÚ¿ª¢½×¶ÎÆoÓᣠÕa¿ÉÒÔ°iÖuÎÒÃÇȶ¨ÎÒÃǵĴuÂeÖеÄÎÊÌa¡£ ×i³£ÓõŦÄÜÊÇ¡°E_ALL¡±£¬ÕaÓÐÖuÓÚÎÒÃÇ¢ÏÖËuÓеľ¯¸aeºÍÑÏÖØ´iÎo¡£ ±ØÐeÖ¸³oµÄÊÇ£¬ÎÒÃÇ°ÑÎÒÃǵĴuÂeͶÈeÉÏÏßÇ°£¬ÎÒÃÇÓ¦¸Ã¹Ø±ÕÕa¸o¹¦ÄÜÌaʾ£¬*ñÔo»aÔÚa¯ÀÀÆ÷Éϵı(C)Â(C)ËuÓÐDZÔÚ´iÎo¼°¾¯¸ae¡£
2£¬Ê¹ÓÃDRYÔÔo
¡®Do not Repeat Yourself'£¬DRYÔÔoÖ¸µÄÊDz»ÒªÖظ´ÄaµÄ´uÂe.¡£ Õa¸o¸ÅÄiÊÇÒ»¸odz£ÓÐÓõıa³Ì¸ÅÄi£¬¿ÉÓ²Ó¦ÓÃÔÚÈκαa³ÌÓiÑÔ£¬ÈçJava£¬C££»oPHP¾u¿ÉÒÔ¡£ ʹÓÃDRYÔÔo£¬È±£ÎÒÃÇÔÚ³ÌÐoÖÐûÓÐÈßÓaµÄ´uÂe¡£
Î¥´ DRY ÔÔoµÄ½a¾o½°¸Í¨³£±»³ÆΪ WET£¬Ö¸´u¡°write everything twice¡±£¬ÎÒÃÇдµÄÏaͬ¹¦ÄܵĴuÂe»a³oÏÖ¶a´ÎÒÔÉÏ£¬±ð´o¼ÒÏ*³ÆÊÇϲ»¶´o×Ö¡£
ÈÃÎÒÃÇ¿´¿´µ½ÏÂÃaeµÄ´uÂe£º
Listing1£º
WET´uÂe*½Ê½£º
$mysql = mysql_connect ( 'localhost', 'mysqladmin_uid', 'mysqladmin_pwd' );
mysql_select_db( 'DB_NAME' ) or die( "Sorry !! No database selected!");
ÏÂÃaeʹÓÃDRY ÔÔoÓÅ»¯´uÂe£º
$db_host = ' localhost ';
$db_user = ' mysqladmin_uid ';
$db_password = ' mysqladmin_pwd ';
$db_database = ' DB_NAME ';
$mysql = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_database);
3£¬Êʵ±µÄʹÓÃËo½ø¼°¿Õ¸ñ
ÔÚʹÓÃÈκÎÒ»ÖÖ±a³ÌÓiÑÔ±aд´uÂeʱ£¬±ØÐeȱ£´uÂeÔÚÐeÒªµÄµØ½Ìa¹(C)ÔÚ±ØÒªÊʵ±µÄËo½øºÍ×a¹»µÄ¿Õ¸ñ¡£ ÒÔÔo¼ÓÁË´uÂeµÄ¿É¶ÁÐÔ£¬ÓøuÓÐЧµÄ*½Ê½¹ÜÀi´uÂe¡£
4£¬Ê¹ÓÃÓÐÒaÒaºÍÒ»ÖµÄÃuÃu±e×¼
ÕýÈçÈκαa³ÌÓiÑÔ£¬PHPµÄÀϱø»¹½¨ÒeÎÒÃÇ×ñÑÓÐÒaÒaµÄÃuÃu±e×¼¡£ ÎÒÃÇÓÐÁ½¸oÖ÷Òª½Ê½ÔÚʵÏÖÕa¸oÒªÇo£º
A.ÍÕaʽ
ÔÚÕaÖÖ½¨ÖУ¬µÚÒ»¸o×ÖĸÊÇСд£¬Ã¿Ò»¸oµ¥´ÊµÄµÚÒ»¸o×ÖĸÆaºoÒÔ´oд¡£
Listing2£º
public class MyClass {
public void methodName(String argName) {
}
}
B.Ï»®Ïßʽ
ÔÚÕaÖÖ½¨ÖУ¬ÎÒÃÇ°ÑÿÁ½¸o´ÊÖ®¼aµÄÏ»®Ïߣ¨¡°_¡±£(C)¡£ ʹÓÃÕaÖÖ½¨Ê±£¬´uÂe¿ÉÒÔ±»ÐÞ¸ÄÈçÏ£º
Listing3£ºÊ¹ÓÃÏ»®Ïß´uÂeƬ¶Î
public class MyClass {
public void method_name(String arg_name) {
}
}
5£¬±ÜÃaÉi²aǶÌ×
ʹÓÃÈκοª¢ÓiÑÔ£¬¶a¼¶Ç¶Ì×½µµÍÁË´uÂeµÄ¿É¶ÁÐÔ¡£ Èκοª¢Õ߶¼Ó¦Ó¦±ÜÃaʹÓÃÉi²aǶÌס£
Listing4£º´uÂeƬ¶Î¾ßÓжa¼¶Ç¶Ì×
<?php
class MyClass {
public function method_name($arg_name) {
if (is_writable ( $folder )) {
if ($fp = fopen ( $file_location_path, 'w' )) {
if ($stuff = extractSomeConditionalStuff ()) {
if (fwrite ( $fp, $stuff )) {
// ...
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
}
?>
ÉÏÃaeµÄ´uÂeÊÇÒ»¸o¼oµ¥µÄǶÌ×´uÂe¡£ ÎÒÃÇ¿ÉÒÔ¿´µ½ËuÊÇ*dz£À§Äѵģ¬Èç¹u¿e½aÊøÄÄÀiµÄÉiÓ°¡£ ΪÁ˸uºÃµÄ¿É¶ÁÐÔ£¬ÈÃÎÒÃÇÐ޸ĵĴuÂe£º
Listing5£º´uÂeƬ¶Î£¬±ÜÃa¶a¼¶Ç¶Ì×
function method_name (String arg_name) {
// ...
if (! is_writable ( $folder )) {
return false;
}
if (! $fp = fopen ( $file_location_path, 'w' )) {
return false;
}
if (! $stuff = extractSomeConditionalStuff ()) {
return false;
}
if (fwrite ( $fp, $stuff )) {
// ...
} else {
return false;
}
}
6£¬Ìi¼ÓÓ¦ÓеÄ×¢ÊÍ
¿ª¢ÖУ¬Çeȱ£ÄaµÄÔ´´uÂeÖÐÓÐ×a¹»µÄÐÐÄÚ×¢ÊÍ¡£ ÕaÊÇÒ»¸oÓ¦¸Ã×ñÊصıe×¼×o¨¡£ ÕaÓÐÖuÓÚÉiÈeÖÎo´uÂe£¬ÒoΪͨ³£Çe¿oÏ£¬¸ºÔð±aÂeµÄÈ˲»»aÒ»Ö±±£³Ö²»±a¡£ ¼´Ê¹Í¬Ò»¸oÈ˱»ÒªÇoÔÚ´uÂeÖнøÐÐÒ»Ð(C)¼oµ¥µÄÐ޸ģ¬ÐÐÄÚµÄ×¢ÊÍ×ÜÊÇ»aÓÐÖuÓÚÀi½aµ±Ê±±aд´uÂeÊÇÓÃÀ´×oʲôµÄ¡£ ΪÁËÔÚPHPÖÐʹÓýϺõÄ×¢Êͱe×¼£¬½¨Òe´o¼ÒÈ¥²e¿´Ò»Ð(C)±e×¼µÄPHPÎĵµ°u£¬ÀýÈçphpDocumentor¡£
7£¬²»Òª°ÑPHPINFO()º¯ÊýÔÚÍøÕ¾¸uĿ¼
phpinfo()º¯ÊýÊÇÒ»¸odz£ÖØÒªµÄ¹¦ÄÜ£¬Ó¦Ð¡ÐÄʹÓᣠʹÓô˹¦ÄÜ£¬ÈκÎÈ˶¼¿ÉÒÔ¿´µ½þÎñÆ÷»¾³µÄÏeϸÐÅÏ¢¡£Ëu×iºÃʼÖÕÅÔÚ
Ò»¸o°²È«Î»ÖõÄÎļþÖÐʹÓã¬Ò»µ(C)¿ª*¢Íe³ÉÉÏÏߺo£¬Ó¦¸Ãɾ³ýÕa¶Î´uÂeʹÓá£
8£¬ÓÀÔ¶²»ÒªÏaÐÅÓû§
Èç¹uÄuµÄÓ¦ÓóÌÐoÉae¼°ÈκÎÓû§ÊaÈe£¬Îñ±ØÒª±aд°²È«µÄ´uÂeÒÔ´¦ÀiÕaÖֽʽ£¬ÒoΪËu°uº¬¸÷ÖÖ¿ÉÄܵÄÊaÈe¡£ ÏeÒª±ÜÃa³ÌÐo±»¹¥»÷×¢Èe»oÕßÆÆ»µÊý¾ÝÍeÕuÐÔ£¬Ò»¶¨ÒªÑeÖ¤¹ýÂËÊý¾ÝµÄ¸ñʽ¡£´o¼Ò¿ÉÒÔ¿´ÏÂÕaƪÎÄÕ¡¶¿ÉŵĴuÂe×¢Èe½Ê½¡*
9£¬ºÏÀiʹÓûº´ae»uÖÆ
Á¼ºÃµÄ±a³Ì½¨×ÜÊǽ¨ÒeʹÓûº´ae»uÖÆ»º´ae°iÖuÎÒÃÇ»ñµÃ¸uºÃµÄÐÔÄÜ¡£
ÔÚPHPµÄÊÀ½ç£¬»º´aeʹÓÃÀ´ÊµÏÖ£º
MemcachedµÄ ¡ª¡ª Ò»¸oÔÚʹÓÃСÊý¾Ý¿e´ae´¢¼uÖµ¶Ô´ae ´¢¡£
APC ¡ª¡ª ¿ÉÑ¡PHP»º´aeΪ¿ª*ŵÄPHP²Ù×÷Âe»º´ae
XCache ¡ª¡ª Ò»ÖÖ¿iËÙ¿É¿¿µÄPHP²Ù×÷Âe»º´ae
Zend Cache ¨C API£¬ÓÃÓÚʵÏÖÏȽøµÄ¸ßËÙ»º´ae¹¦Äܵļ¯ºÏ¡£
eAcclerator ¨C ¿ªÔ´µÄ»º´ae¹¤¾ß
10£¬±ÜÃa¸´ÖÆÒÑÓбaÁ¿
½«Ô¤¶¨Òa±aÁ¿¸´ÖƵ½¾ßÓнÏСµÄÃu×ֵľֲ¿±aÁ¿²»ÊÇÒ»¸oºÃµÄ±a³ÌÏ°¹ß¡£ Õa¶ÔÓ¦ÓóÌÐoµÄÐÔÄܲuÉu²»ÀuÓ°Ïi¡£ ÈÃÎÒÃÇÀ´¿´¿´ÏÂÃaeµÄ´uÂeƬ¶Î£º
Listing6£º¸´ÖÆÒÑÓбaÁ¿
$desc = strip_tags($_POST['PHP description']);
echo $desc;
ÉÏÃaeµÄ´uÂeµÄ´uÂeÊǸ´ÖÆÒ»¸o±aÁ¿Îª¾Ö²¿±aÁ¿²»±ØÒªµÄÀý×Ó¡£ ÕaÊDz»ÊÇÒ»¸oºÃµÄ×o*¨¡£Í¬ÑuµÄЧ¹u¿ÉÒÔͨ¹ýʹÓÃÏÂÃaeµÄ´uÂeÀ´ÊµÏÖ£º
echo strip_tags($_POST['PHP description']);
11£¬Ê¹Óÿo¼Ü
¿o¼ÜÊÇ´oÁ¿µÄÑо¿ºo¿ª¢µÄ£¬Òo´Ë£¬ËuÃDZ»Ö¤Ã÷ÊÇÎÊÌa½ÏÉÙ¡£ ËuÃÇʹÎÒÃǵĿª¢¸u±a½Ý£¬ÒoΪËuÃÇÌa¹(C)³ÉÊiµÄ½a¾o½°¸¡£ ÔÚPHPÖÐÓкܶa¿ÉÓõĿo¼Ü¡£ ÔÚ¿ª¢¹ý³ÌÖУ¬ÄaÓ¦¸ÃÀuÓÃÕaÐ(C)¡£ ÆaÖеÄÒ»¸o¿o¼Ü£¬±»¹a*º²ÉÓõÄÊÇMVC»oÄ£ÐÍÊÓͼ¿ØÖÆÆ÷¡£
½aÂÛ£º
±a³Ì¹ae¶Ö¸µ¼ÎÒÃǸuÓÐЧµÄ¿ª¢´uÂe¡£
×ñѱa³Ì¹ae¶£¬È±£Ó¦ÓóÌÐoµÄ¸uºÃµÄÐÔÄÜ¡£
ÕýÈçÔÚÆaËu±a³ÌÓiÑÔ£¬ÒªÏe´´Ôi³o¸ßÖÊÁ¿µÄ´uÂe£¬PHPÒ²ÐeÒª×ñÑÕaÖÖÁ¼ºÃµÄ±a³Ì¹ae*¶¡£