PHP
·
发表于 5年以前
·
阅读量:8296
±¾ÎÄ»a×ÜÁËPHPѧϰÖг£¼uµÄ¸÷ÀaÎÊÌa£¬Ô¼ÓÐÁ½Ç§¶aÐдuÂe£¬¶¼ÊÇdz£ÊµÓõļ¼ÇÉ¡£ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼¡£¾ßÌaÈçÏ£º
//Ói*¨´iÎo£¨syntax error£(C)ÔÚÓi*¨*ÖÎo½×¶Î£¬Ô´´uÂe²¢Î´±»Ö´ÐУ¬¹Ê²»»aÓÐÈκÎÊa³o¡£
/* ¡¾ÃuÃu¹aeÔo¡¿ */
³£Á¿Ãu Àa³£Á¿½¨ÒeÈ«´oд£¬µ¥´Ê¼aÓÃÏ»®Ïß*Ö¸o // MIN_WIDTH
±aÁ¿Ãu½¨ÒeÓÃÏ»®Ïß*½Ê½*Ö¸o // $var_name
º¯ÊýÃu½¨ÒeÓÃÍÕ*aÃuÃu*¨ // varName
¶¨½ç*u½¨ÒeÈ«´oд // <<<DING, <<<'DING'
ÎļþÃu½¨ÒeȫСдºÍÏ»®Ïß¡¢Êý×Ö // func_name.php
˽ÓÐÊoÐÔÃu¡¢*½*¨Ãu½¨Òe¼ÓÏ»®Ïß // private $_name _func
½Ó¿ÚÃu½¨Òe¼ÓI_ // interface I_Name
/* ÓiÑÔ½a¹¹ */
array(), echo(), empty(), eval(), exit(), isset(), list(), print(), unset()
echo, print ¿ÉÊ¡ÂÔÀ¨ºÅ¡£
/* Ô¤¶¨Òa³£Á¿ */
PATH_SEPARATOR //Â*¾¶*Ö¸o*u(WindowsΪ*ֺţ¬ÀaUnixΪðºÅ)
DIRECTORY_SEPARATOR //Ŀ¼*ָo*u
PHP_EOL //µ±Ç°ÏµÍ³µÄ»»ÐÐ*u
PHP_VERSION //PHP°ae±¾ºÅ
PHP_OS //PHP*þÎñ²Ù×÷ϵͳ
PHP_SAPI //ÓÃÀ´ÅжÏÊÇʹÓÃÃuÁiÐл¹ÊÇa¯ÀÀÆ÷Ö´Ðеģ¬Èç¹u PHP_SAPI=='cli' ±iʾÊÇÔÚÃuÁiÐÐÏÂÖ´ÐÐ
PHP_INT_MAX INT×i´oÖµ£¬32λƽ̨ʱֵΪ2147483647
PHP_INT_SIZE INT×Ö³¤£¬32λƽ̨ʱֵΪ4£¨4×Ö½Ú£(C)
M_PI //Ô²ÖÜÂÊÖµ
M_E //×ÔÈ»Êý
//PHPÔËÐл*¾³¼i²aº¯Êý
php_sapi_name() //*µ»ØÒ»¸oPHPÓeWEB*þÎñÆ÷½Ó¿ÚÀaÐ͵ÄСд×Ö*u´®
¸Ãº¯Êý*µ»ØÖµÓe³£Á¿PHP_SAPIÒ»Ö£¡
½Ó¿ÚÀaÐÍ£ºSAPI(the Server API, SAPI)
¿ÉÄÜÖµ£ºaolserver¡¢apache¡¢apache2filter¡¢apache2handler¡¢caudium¡¢cgi¡¢cgi-fcgi¡¢cli¡¢ continuity¡¢embed¡¢isapi¡¢litespeed milter¡¢nsapi¡¢phttpd¡¢pi3web¡¢roxen¡¢thttpd¡¢tux¡¢webjames
/* ´oСдÎÊÌa */
- ÀaÃu¡¢*½*¨Ãu¡¢ÊoÐÔÃu¡¢º¯ÊýÃu£º²»Çø*Ö´oСд
- ±aÁ¿Ãu¡¢³£Á¿Ãu¡¢ÔªËØϱe£ºÇø*Ö´oСд
/* ¿É±a±eʶ*u */
¿É±a±aÁ¿ $i = 3; $k = 'i'; echo $$k; //Êa³o3
¿É±aº¯Êý function func() {echo 'hello!';} $i = 'func'; $i(); //Êa³ohello
¿É±aϱe $i = '1234'; $k = 3; echo $i[$k]; //Êa³o4
¿É±aÀaÃu class CLS{public $k = 'hello';} $i = 'CLS'; $j = new $i; echo $j->k;
¿É±aÊoÐÔ class CLS{public $k = 'hello';} $i = 'k'; $j = new CLS; echo $j->$i;
¿É±a*½*¨ class CLS{public function k(){echo 'hello';}} $i='k'; $j=new CLS; $j->$i();
/* ¿É±a±aÁ¿ */
* ÓÃÓÚÒµÎñÂß¼Åжϵõ½Ä³Ð(C)¾ßÌaÐÅÏ¢
$var_name = "class_name";
$$var_name = "PHP0913"; // $class_name = "PHP0913";$class_nameÒÑ´aeÈeÄÚ´aeÖÐ
var_dump($class_name); // var_dump($$var_name);
/* ±aÁ¿º¯Êý */
get_defined_vars //*µ»ØÓÉËuÓÐÒѶ¨Òa±aÁ¿Ëu×e³ÉµÄÊý×e(°uÀ¨»*¾³±aÁ¿¡¢*þÎñÆ÷±aÁ¿ºÍÓû§¶¨ÒaµÄ±aÁ¿)
/* unset() */
* unset()½oɾ³ýµ±Ç°±aÁ¿ÃuºÍÒýÓã¬ÆaÖµ²¢Î´±»É¾³ý
* ÒýÓô«µÝÖУ¬É¾³ýÒ»¸o±aÁ¿¼°ÆaÒýÓã¬ÆaËu±aÁ¿¼°ÒýÓþu´aeÔÚ£¬ÇÒÖµÒÀÈ»´aeÔÚ
echo "<br />";
$v3 = 'Öµ';
$v4 = &$v3;
unset($v4);
var_dump($v3, $v4);
/* ±aÁ¿µÄ×i³¤ÓÐЧÆÚ */
* µ±Ç°½Å±¾µÄÖ´ÐÐÖÜÆÚ£¬½Å±¾Ö´ÐнaÊø£¬±aÁ¿¼´Ïuʧ
/* Ô¤¶¨Òa±aÁ¿/³¬È«¾Ö±aÁ¿ */
$GLOBALS
$_COOKIE
$_ENV
$_FILES
$_GET
$_POST
$_REQUEST
$_SERVER
$_SESSION
/* ³£Á¿¶¨Òa */
define(³£Á¿Ãu, ³£Á¿Öµ, [Çø*Ö´oСд²ÎÊý]) //true±iʾ²»Çø*Ö/false±iʾÇø*Ö´oСд
const ³£Á¿Ãu = ³£Á¿Öµ // У¬½¨Òe
³£Á¿Ãu¿ÉÒÔʹÓÃÌØÊa×Ö*u
constant($name) // »ñÈ¡³£Á¿Ãu
// Àý£ºecho constant('-_-');
/* ³£Á¿Ïa¹Øº¯Êý */
defined
get_defined_constants
/* Ô¤¶¨Òa³£Á¿ */
__FILE__ ËuÔÚÎļþµÄ¾ø¶ÔÂ*¾¶
__LINE__ ÎļþÖеĵ±Ç°ÐкÅ
__DIR__ ÎļþËuÔÚĿ¼
__FUNCTION__ º¯ÊýÃu³Æ
__CLASS__ ÀaµÄÃu³Æ
__METHOD__ ÀaµÄ*½*¨Ãu
__NAMESPACE__ µ±Ç°ÃuÃu¿Õ¼aµÄÃu³Æ
/* ÕuÐÍ */
ÕuÐÍÕ¼ÓÃ4×Ö½Ú£¬¹²4*8=32룬×i´oֵΪ2147483647£¬×iСֵΪ-2147483648£¬×iСֵµÄ¾ø¶ÔÖµ±È×i´oÖµµÄ´o1
×i¸ßΪ±iʾÕý¸º£¬1±iʾ¸º£¬0±iʾÕý
/* ½øÖÆת»»º¯Êý */
Ö»ÄÜÊ®½øÖÆÓeÆaËu½øÖƽøÐÐת»»£¬Ö»ÓÐÁuÖÖ
ת»»Ê±£¬²ÎÊýÓ¦ÊÇ×Ö*u´®£¨¼´²»¿Éº¬°Ë½øÖƵġ°0¡±»oÊ®Áu½øÖƵġ°0x¡±£(C)
dec
bin
oct
hex
hexdec() Ê®Áu½øÖÆתʮ½øÖÆ Ò²¿Éдhex2dec()
dechex() Ê®½øÖÆתʮÁu½øÖÆ Ò²¿Éдdec2hex()
bindec() ¶þ½øÖÆתʮ½øÖÆ Ò²¿Éдbin2dec()
decbin() Ê®½øÖÆת¶þ½øÖÆ Ò²¿Éдdex2bin()
octdec() °Ë½øÖÆתʮ½øÖÆ Ò²¿Éдoct2dec()
decoct() Ê®½øÖÆת°Ë½øÖÆ Ò²¿Éдdec2oct()
/* ¸¡µaÊý */
¸¡µaÊý²»ÄܱȽϴoС £¡£¡£¡
¼¸ºoËuÓÐСÊý£¬ÔÚ±£´aeʱ¶¼ÊǽuËÆÖµ¶ø²»ÊǾ«È*Öµ£¡
×i´oÖµ£º+/- 1.8E308
PHPËuÄܱ£´aeµÄ×i³¤Ð¡Êýλ£º14λ
/* µ¥ÒýºÅ×Ö*u´® */
µ¥ÒýºÅ×Ö*u´®ÖУ¬Ö»ÄÜתÒa*´Ð±¸ÜºÍµ¥ÒýºÅ
/* Ë«ÒýºÅ×Ö*u´® */
Ö»½aÎo×Ö*u´®Ò»´Î £¡£¡£¡
eval °Ñ×Ö*u´®×÷ΪPHP´uÂeÖ´ÐÐ
´oÀ¨ºÅ°u¹u±aÁ¿£¬¿ÉÈ*¶¨±aÁ¿Ãu½çÏÞ¡£È磺"aaa{$bbb}ccc"
Ë«ÒýºÅÖпÉÒÔ½«ASCIIÂeת»»Îª×Ö*u
"\x61" -> a // ×Ö*u´®Öв»Ðe0£¬ÕuÐÍÖвÅÊÇ0xÇ°µ¼
"\x49\x54\x43\x41\x53\x54" -> ITCAST
½«ASCIIת³É×Ö*uº¯Êýchr()
½«×Ö*uת³ÉASCIIº¯Êýord()
#Ë«ÒýºÅתÒaÁбi
\n »»ÐÐ
\r »Ø³µ
\t ˮƽÖƱi*u
\\ *´Ð±Ïß
\$ ÃÀÔª±e¼Ç
\v ´¹Ö±ÖƱi*u
\e Escape
\f »»Ò³
\" Ë«ÒýºÅ"
\[0-7]{1,3} *uºÏ¸ÃÕýÔo±i´iʽÐoÁеÄÊÇÒ»¸oÒ԰˽øÖÆ*½Ê½À´±i´iµÄ×Ö*u
\x[0-9A-Fa-f]{1,2} *uºÏ¸ÃÕýÔo±i´iʽÐoÁеÄÊÇÒ»¸oÒÔÊ®Áu½øÖÆ*½Ê½À´±i´iµÄ×Ö*u
/* ¶¨½ç*u */
herodoc - ¹¦ÄÜͬ˫ÒýºÅ£¬ÄܽaÎo
$str = <<<AAA
×Ö*u´®ÄÚÈÝ
AAA;
nowdoc - ¹¦ÄÜͬµ¥ÒýºÅ£¬²»ÄܽaÎo
Ö»ÔÚ¿ªÊ¼Î»ÖÃÓе¥ÒýºÅ
$str = <<<'AAA'
×Ö*u´®ÄÚÈÝ
AAA;
/* ×Ö*u´®µÄʹÓà */
¿É½«×Ö*u´®µ±×÷Ò»¸o×Ö*uµÄ¼¯ºÏÀ´Ê¹Ó㬿ɶÀÁ¢*ÃÎÊÿ¸o×Ö*u¡£½oÊÊÓÃÓÚµ¥×Ö½Ú×Ö*u£¨×Öĸ¡¢Êý×Ö¡¢°e½Ç±eµa*uºÅ£(C)£¬ÏñÖÐÎĵȲ»¿ÉÓÃ
$str = "abcd";
echo $str[3]; // d
echo $str{0}; // a
/* ¡¾ÀaÐͲÙ×÷º¯Êý¡¿ */
//»ñÈ¡/ÉeÖÃÀaÐÍ
gettype($var) //»ñÈ¡±aÁ¿µÄÊý¾ÝÀaÐÍ
settype($var, $type) //ÉeÖñaÁ¿µÄÊý¾ÝÀaÐÍ
//ÀaÐÍÅжÏ
is_int
is_float
is_null
is_string
is_resource
is_array
is_bool
is_object
is_numeric ¼i²a±aÁ¿ÊÇ*ñΪÊý×Ö»oÊý×Ö×Ö*u´®
//ת»»³ÉÖ¸¶¨µÄÊý¾ÝÀaÐÍ
boolval
floatval
intval
strval
//Ç¿ÖÆת»»ÀaÐÍ
(int)
(float)
(string)
(bool)
(array)
(object)
(unset) //ת»»ÎªNULL
(binary) ת»»ºÍ bǰ׺ת»» //ת»»³É¶þ½øÖÆ
var_dump ´oÓ¡±aÁ¿µÄÏa¹ØÐÅÏ¢¡£
ÏÔʾ¹ØÓÚÒ»¸o»o¶a¸o±i´iʽµÄ½a¹¹ÐÅÏ¢£¬°uÀ¨±i´iʽµÄÀaÐÍÓeÖµ¡£
Êý×e½«µÝ¹eÕ¹¿ªÖµ£¬Í¨¹ýËo½øÏÔʾÆa½a¹¹¡£
var_export($var [,bool $return]) //Êa³o»o*µ»ØÒ»¸o±aÁ¿µÄ×Ö*u´®±iʾ
$return£ºÎªtrue£¬Ôo*µ»Ø±aÁ¿Ö´ÐкoµÄ½a¹u
print_r ´oÓ¡¹ØÓÚ±aÁ¿µÄÒ×ÓÚÀi½aµÄÐÅÏ¢
empty ¼i²eÒ»¸o±aÁ¿ÊÇ*ñΪ¿Õ
isset ¼i²a±aÁ¿ÊÇ*ñ´aeÔÚ
/* ¡¾Á÷³Ì¿ØÖÆ¡¿ */
//ifÓi¾aµÄÌae´uÓi*¨
if (Ìo¼þÅжÏ) :
Ói¾a¿e;
elseif (Ìo¼þÅжÏ) :
Ói¾a¿e;
else :
Ói¾a¿e;
endif;
//Á÷³Ì¿ØÖƵÄÌae´uÓi*¨
ÔÚǶÈeHTMLʱ³£ÓÃ
½« { »»³É : , ½« } »»³É endif; µÈ
endif
endwhile
endfor
endforeach
endswitch
/* ¡¾switch¡¿ */
switch (Ìo¼þ) {
case ״ֵ̬1:
Ói¾a¿e;
[break;]
case ״ֵ̬2:
Ói¾a¿e;
[break;]
case ״ֵ̬3:
case ״ֵ̬4:
Ói¾a¿e;
[break;]
default:
Ói¾a¿e;
[break;]
}
switchÊÇ״̬*ÖÖ§£¬ÌØÊaµÄÑ»*
ÏȼÆËa³o״ֵ̬£¬ÔÙÈ¥ÓeÅжÏÊý×÷±È½Ï
breakÍ˳oÁ÷³Ì
/* ¡¾forÑ»*¡¿ */
for (Ìo¼þ³oʼ»¯±i´iʽ; Ìo¼þÅжϱi´iʽ; Ìo¼þ±a»¯±i´iʽ) {
Ñ»*Ìa
}
¼ÙÉeÑ»*Ìa±»Ö´ÐÐÁËN´Î£¬Ôo
Ìo¼þ³oʼ»¯±i´iʽ±»Ö´ÐÐ1´Î
Ìo¼þÅжϱi´iʽ±»Ö´ÐÐN+1´Î
Ìo¼þ±a»¯±i´iʽ±»Ö´ÐÐN´Î
×¢Òa£º
1. Ñ»*±aÁ¿ÔÚforÓi¾a½aÊøºo»¹¿ÉÒÔ¼ÌÐøʹÓã¬ÖµÎªµÚÒ»´Îʧ°ÜµÄÖµ
2. Ñ»*±aÁ¿ÔÚforÑ»*ÌaÄÚ¿ÉÒÔʹÓÃ
3. ÈκÎÌo¼þ±i´iʽ¾u¿ÉÊ¡ÂÔ£¬µ«*ֺŲ»ÄÜÊ¡ÂÔ
a. Ìo¼þ³oʼ»¯±i´iʽ±»Ê¡ÂÔʱ£¬Ñ»*±aÁ¿±»¸³ÖµÎªnull£¬ÔÚÓeÌo¼þÅжÏʱ£¬
½øÐÐÀaÐÍת»»ºoÔٱȽϡ£Ò²¿ÉÒÔÔÚforÓi¾aÍa½øÐгoʼ»¯¡£
b. Ìo¼þÅжϱi´iʽ±»Ê¡ÂÔʱ£¬±iʾѻ*ΪÕae£¬½øÈeËÀÑ»*
c. Ìo¼þ±a»¯±i´iʽ±»Ê¡ÂÔʱ£¬¿ÉÒÔÔÚÑ»*ÌaÄÚÍe³É
4. ÿ¸o±i´iʽ¾u¿ÉÓɶaÌoÓi¾a×e³É£¬Ã¿ÌoÓi¾aÖ®¼aʹÓöººÅ*Ö¸i
Èç¹uÌo¼þÅжϱi´iʽÓɶaÌoÓi¾a×e³É£¬¶¼»aÖ´ÐУ¬µ«Ö»ÓÐ×iºoÒ»ÌoÓi¾a²Å×÷ΪÅжÏÌo¼þ
5. forÖ»ÄܱeÀuÊýÖµÐÍË÷ÒýϱeÊý×e
Êý×e³¤¶Èº¯Êý£ºcount()
6. Ó¦¸Ã½«¿ÉÒÔ³oʼ»¯µÄÓi¾a¾u*ÅÔÚÌo¼þ³oʼ»¯±i´iʽÄÚ£¬ÕaÑu¿ÉÒÔÊ¡È¥ºÜ¶aÖ´ÐдÎÊý
/* ¡¾goto¡¿5.3+ °ae±¾ */
ÓÃÀ´Ìøתµ½³ÌÐoÖеÄijһָ¶¨Î»ÖÃ
¸ÃÄ¿±eλÖÿÉÒÔÓÃÄ¿±eÃu³Æ ¼ÓÉÏðºÅÀ´±e¼Ç¡£
PHPÖеÄgotoÓÐÒ»¶¨ÏÞÖÆ£¬Ö»ÄÜÔÚͬһ¸oÎļþºÍ×÷ÓÃÓoÖÐÌøת£¬
Ò²¾ÍÊÇ˵ÄaÎÞ*¨Ìø³oÒ»¸oº¯Êý»oÀa*½*¨£¬Ò²ÎÞ*¨ÌøÈeµ½ÁiÒ»¸oº¯Êý¡£
ÄaÒ²ÎÞ*¨ÌøÈeµ½ÈκÎÑ»*»oÕßswitch½a¹¹ÖС£
³£¼uµÄÓÃ*¨ÊÇÓÃÀ´Ìø³oÑ»*»oÕßswitch£¬¿ÉÒÔ´uÌae¶a²aµÄbreak¡£
¿ÉÒÔ´ÓÑ»*(switch)ÖÐÌø³oÀ´£¬µ«²»ÄÜ´ÓÍa²¿Ìøת½øÈ¥¡£¶øº¯Êý»oÀa*½*¨£¬ÏoÍaÏoÄÚ¾u²»¿É¡£
goto a;
echo 'Foo';
a:
echo 'Bar';
/* ¡¾Îļþ¼ÓÔØ¡¿ */
require / include / require_once / include_once
ÎļþÔØÈeÖ»ÊÇÔØÈeÄ¿±eÎļþÄڵĴuÂe²¢Ö´ÐУ¬ÓeÔØÈeµÄÎļþÀaÐÍÎÞ¹Ø
ÎļþÔØÈeÊoÓÚÖ´Ðн׶Σ¬µ±Ö´Ðе½requireµÈÓi¾aʱ£¬²ÅÔØÈe¸ÃÎļþµÄ´uÂe£¬
±aÒe²¢Ö´ÐУ¬È»ºo»Øµ½requireµÈÓi¾aλÖüÌÐøÖ´ÐÐÏÂÃaeµÄÓi¾a
¡¾×¢Òa¡¿
ÔÚÔØÈe¿ªÊ¼Ê±£¬ÏÈÍ˳oPHPģʽ£»
ÔÙÔØÈeÄ¿±eÎļþ´uÂe£¬Ö´ÐиôuÂe£»
½aÊøʱ£¬ÔÙ½øÈePHPģʽ¡£
require£º´¦Àiʧ°Ü£¬²uÉu E_COMPILE_ERROR ´iÎo£¬½Å±¾ÖÐÖ¹¡£
include£º´¦Àiʧ°Ü£¬²uÉu E_WARNING ´iÎo£¬½Å±¾¼ÌÐøÖ´ÐС£
#²»½¨ÒeʹÓÃrequire_once/include_once
/* ¡¾Ïa¶ÔÂ*¾¶¡¿ */
µ±Ç°a¯ÀÀÆ÷ÇeÇoµÄÄĸo½Å±¾£¬µ±Ç°Î»ÖþÍÊÇÊoÓÚÄĸo½Å±¾¡£
./file ºÍ file ¶¼±iʾµ±Ç°Ä¿Â¼ÏµÄfileÎļþ
fileÇe¿o£¨Ç¶Ì×ÔØÈeÎļþʱ£(C)£º
Èç¹uµ±Ç°Ä¿Â¼Ã»ÕÒµ½¸ÃÎļþ¾ÍÔÚ´uÂeÎļþËuÔÚĿ¼ÖмÌÐøÕÒ¡£
Èç¹uµ±Ç°Ä¿Â¼ÕÒµ½ÓиÃÎļþ£¬Ôo²»»aÔÙÔÚ´uÂeÎļþËuÔÚĿ¼ȥÕÒÒ²²»»aÔÙ¼ÓÔØ¡£
__DIR__ ½Å±¾ÎļþËuÔÚĿ¼
__FILE__ ½Å±¾ÎļþÂ*¾¶
include_path ¼ÓÔØÎļþ²eÕÒĿ¼
set_include_path() ÉeÖÃinclude_path£¬¿É¶a¸o£¬ÓÃ×Ö*u´®×÷²ÎÊý
¸Ãº¯ÊýÉeÖõÄpathÖ»Õe¶Ô¸Ãµ±Ç°ÎļþÓÐЧ
¸ÃÉeÖÃÖ»Õe¶Ô²eÕÒδֱ½ÓдÎļþÂ*¾¶*½Ê½ÓÐЧ
ÉeÖÃеÄinclude_path»a¸²¸ÇÔÀ´µÄ
get_include_path() »ñÈ¡µ±Ç°include_pathÉeÖÃÏi£¬ÎÞ²ÎÊý
Â*¾¶*Ö¸o*u£¬ÔÚWindowsÏÂÊÇ*ֺţ¬ÔÚLinuxÏÂÊÇðºÅ
ÀuÓÃÔ¤¶¨Òa³£Á¿ PATH_SEPARATOR À´»ñµÃµ±Ç°µÄ*Ö¸o*u
Èç¹uÖ±½ÓдÎļþÃu£º
1. include_pathËuÉeÖõÄ
2. µ±Ç°Ä¿Â¼
3. ´uÂeËuÔÚÎļþµÄĿ¼
Èç¹uÎļþÃuÇ°´øÓÐÂ*¾¶£¬Ôo»aÖ±½Ó¸u¾ÝÂ*¾¶²eÕÒ£¬include_pathÖ±½Ó±»ºoÂÔ
/* ¡¾return¡¿ */
returnÓerequire½aºÏ£¬¿É*µ»ØÎļþµÄÄÚÈÝ£¬returnдÔÚ±»ÔØÈeµÄÎļþÄÚ
return¿ÉÒÔÖÕÖ¹ËuÔڽű¾µÄÖ´ÐУ¬×÷ΪÆÕͨ½Å±¾Ói¾a
return¿ÉÒÔ*µ»Øº¯ÊýµÄÏaÓ¦Öµ
/* ¡¾ÖÕÖ¹ºÍÑӳٽű¾Ö´ÐС¿ */
die / exit ÖÕÖ¹
returnÊÇÖÕÖ¹ËuÔڽű¾µÄÖ´ÐÐ
dieºÍexit»aÁ¢¼´ÖÕÖ¹½Å±¾Ö´ÐÐ
die("µ½´ËΪֹ"); ¸Ãº¯ÊýÄÚµÄ×Ö*u´®¿É±»Êa³o
sleep() ÑÓ³Ù(µ¥Î»£ºÃe)
ĬÈÏ×i¶a¿ÉÑÓ³Ù30Ãe£¬PHPÅaÖÿÉÒÔÐÞ¸Ä max_execution_time
Àý£ºsleep(12);
usleep() ÒÔÖ¸¶¨µÄ΢ÃeÊýÑÓ³ÙÖ´ÐÐ
time_sleep_until ʹ½Å±¾Ë¯Ãßµ½Ö¸¶¨µÄʱ¼aΪֹ
/* ¡¾º¯Êý¡¿ */
1. º¯ÊýµÄÉuÃ÷ÊÇÔÚ±aÒeʱ£¬¹ÊÏȶ¨ÒaÔÙµ÷Ó㬶¨ÒaÓeµ÷ÓÃÎÞÏȺo¹Øϵ£¡
2. ÎļþÖ»ÊÇ´uÂeµÄÔØÌa£¬³ÌÐo¾uÔÚÄÚ´aeÖÐÖ´ÐУ¡
3. Èç¹uº¯ÊýµÄ¶¨ÒaÔÚÐeÒªÔØÈeµÄÎļþÄÚ£¬ÔoÐeÒªÏÈÔØÈe¸ÃÎļþ£¬*ñÔoµ÷Óóo´i£¡
4. º¯ÊýµÄ¶¨Òa¿ÉÒÔ³oÏÖÔÚÆaËuµÄ´uÂe¶ÎÖУ¬´Ëʱº¯Êý²»»aÔÚ±aÒe½×¶Î±»Ö´ÐÐ
Ö»Óб»Ö´Ðе½Ê±²Å»a±»¶¨Òa£¡Ö»ÓжÀÁ¢¶¨Òaʱ²Å»a±»±aÒeÔÚÄÚ´aeÖУ¡
Èç¹u³oÏÖÔÚÆaËuº¯ÊýÌaÄÚ£¬Ò²ÐeÒªÍa²aº¯Êý±»µ÷ÓÃʱ²Å±»¶¨Òa²¢ÉuЧ£¡
5. º¯ÊýÃu²»Çø*Ö´oСд
6. ²»ÔÊÐiÖØÃu£¬°uÀ¨ÏµÍ³º¯Êý
7. ¡¾¿É±aº¯Êý¡¿
º¯ÊýÃu¿ÉÒÔÓÃÆaËu±aÁ¿´uÌae
$func_name = "sayHello";
$func_name(); //´Ëʱµ÷ÓÃsayHello()º¯Êý
×¢Òa£ºÖ»ÓÐÔÚµ÷ÓÃʱ²ÅÄÜʹÓñaÁ¿£¬¶¨Òaʱ²»ÔÊÐi£¡
8. ±aÁ¿¿É×÷Ϊº¯ÊýÃuµ÷Óú¯Êý£¬Êý×eÔªËØÖµÒ²¿ÉÒÔ£¡
9. ÐÎʽ²ÎÊýparameter£¬Êµ¼Ê²ÎÊýargument
¿ÉÒÔ¶Ô²ÎÊý´«µÝ null£¬±iʾ¸ÃÐββ»Ïe´«µÝÖµ
ÐβÎÓeʵ²ÎÖ®¼a¼È¿ÉÖµ´«µÝ£¬Ò²¿ÉÒýÓô«µÝ¡£
ÒýÓô«µÝ²ÎÊý£¬Ó¦¸ÃÔÚ¶¨Òaº¯Êýʱ¾ÍÔÚÐÎʽ²ÎÊýÇ°¼ÓÉÏ & *uºÅ£¬¶ø´Ëʱµ÷Óú¯Êýʵ²Î±ØÐeΪ±aÁ¿
ÈçºÎÑ¡ÔñʹÓÃÄÄÖÖ´«µÝ*½Ê½£¿
a. ÊÇ*ñÐeÒª±£Ö¤ÔʼÊý¾ÝµÄÍeÕuÐÔ
b. ÊÇ*ñÐeÒªÔo¼ÓЧÂÊ
c. ¶Ô´oÊý¾ÝÒýÓô«µÝ¿É½ÚÊ¡ÄÚ´ae
10. ²ÎÊýĬÈÏÖµ
a. º¯ÊýµÄ²ÎÊýĬÈÏÖµ±ØÐeÊÇÒѾÈ*¶¨µÄÖµ£¬²»ÄÜÊDZaÁ¿£¡
Ö»ÒªÔÚµ÷ÓÃ֮ǰ¶¨Òa¸Ã³£Á¿£¬Ôo¿ÉÒÔʹÓó£Á¿×÷Ϊ²ÎÊýĬÈÏÖµ
b. º¯ÊýĬÈÏÖµ¿ÉÒÔÓжa¸o£¬½¨Òe½«ÓÐĬÈÏÖµµÄ²ÎÊý*ÅÔÚ²ÎÊýÁбiµÄ×iºoÃae
ÕaÑu¿ÉÒÔÔÚµ÷Óú¯Êýʱ£¬²»¸³ÓeºoÃaeÓÐĬÈÏÖµµÄ²ÎÊýÖµ£¬*ñÔo»a³o´i
c. ĬÈϲÎÊý¿ÉÒÔÊÇ*DZeÁ¿ÀaÐÍ£¬±ÈÈçÊý×e¡¢null
d. ÈκÎĬÈϲÎÊý±ØÐe*ÅÔÚÈκÎ*ÇĬÈϲÎÊýµÄÓÒ²a
11. ²ÎÊýÊýÁ¿
a. ÐβÎÊýÁ¿¶aÓÚʵ²ÎÊýÁ¿
±¨¸ae¾¯¸ae¼¶±ð´iÎo£¬²¢ÒÔNULL´uÌae
b. ʵ²Î¶aÓÚÐβÎ
²»±¨¸ae´iÎo£¬ÒÀ´ÎΪÐβθ³Öµ
c. ²»È*¶¨²ÎÊýÊýÁ¿
1) Ò»¸oÐβζ¼²»¶¨Òa£¬ÓÀÔ¶¶¼ÊÇʵ²Î¶aÓÚÐβÎ
2) ¡¾¿É±aÊýÁ¿²ÎÊý¡¿
func_get_args() »ñÈ¡µ±Ç°º¯Êý±»µ÷ÓÃʱËuÓÐʵ²ÎµÄÖµ£¬*µ»ØÒ»¸oËuÓÐʵ²ÎÖµ×e³ÉµÄÊý×e
func_get_arg() »ñȡij¸oʵ²ÎµÄÖµ£¬Í¨¹ýË÷ÒýÖµ±eʶ£¬e.g: func_get_arg(0)
func_num_args() »ñÈ¡ËuÓÐʵ²ÎµÄÊýÁ¿
12. ¡¾return¡¿*µ»ØÖµ
a. º¯ÊýÖ»ÓÐÒ»¸o*µ»ØÖµ£¬¿ÉÒÔͨ¹ý*µ»ØÒ»¸oÊý×eÀ´µÃµ½ÀaËƵĽa¹u£¬µ«¿ÉÒÔÓжaÌoreturnÓi¾a
b. returnÓi¾a»aÁ¢¼´ÖÐÖ¹º¯ÊýµÄÔËÐУ¬²¢½«¿ØÖÆȨ½»»Øµ÷Óøú¯ÊýµÄ´uÂeÐÐ
c. ¿ÉÒÔ*µ»Ø°uÀ¨Êý×eºÍ¶ÔÏoµÄÈÎÒaÀaÐÍ
d. º¯ÊýµÄ*µ»ØÒ²*ÖÖµ´«µÝºÍÒýÓô«µÝ£¨*µ»ØµÄÊÇÒ»¸o±aÁ¿²Å¿É£(C)
1) ĬÈÏÊÇÖµ´«µÝ*½Ê½
2) ÒýÓô«µÝ*½Ê½£º
- ¶¨Òaº¯Êýʱ£¬º¯ÊýÃuÇ°¼ÓÉÏ& ±iʾ¸Ãº¯Êý¿ÉÒÔ*µ»ØÒýÓÃ
- µ÷Óú¯Êýʱ£¬º¯ÊýÃuÇ°¼ÓÉÏ& ±iʾȡµÃº¯Êý*µ»ØµÄÒýÓÃ
´Ëʱ£¬º¯ÊýÍaÐÞ¸Ä*µ»ØÖµ£¬»aÐ޸ĺ¯ÊýÄڵĸÃ*µ»Ø±aÁ¿µÄÖµ
- Èç¹uº¯ÊýÐe*µ»ØÒýÓã¬ÔoÐeÒª*µ»ØÒ»¸o±aÁ¿²Å¿ÉÒÔ
- ´Óº¯Êý*µ»ØÒ»¸oÒýÓ㬱ØÐeÔÚº¯ÊýÉuÃ÷ºÍÖ¸ÅÉ*µ»ØÖµ¸øÒ»¸o±aÁ¿Ê±¶¼Ê¹ÓÃÒýÓòÙ×÷*u&
function &returns;_reference(){return $someref;}
$newref =& returns_reference();
3) *µ»ØÒýÓõÄ×÷ÓÃ
/* ¡¾±aÁ¿×÷ÓÃÓo¡¿ */
a. È«¾Ö±aÁ¿ºÍ¾Ö²¿±aÁ¿
1) ×÷ÓÃÓoÖ®¼a²»Öصþ£¬¼´²»Í¬×÷ÓÃÓoµÄ±aÁ¿£¬Ö®¼a²»¿É*ÃÎÊ
2) È«¾Ö×÷ÓÃÓo - º¯ÊýÖ®ÍaµÄÇøÓo
3) ¾Ö²¿×÷ÓÃÓo - º¯ÊýÄÚµÄÇøÓo£¬Ã¿¸oº¯Êý¶¼ÊÇÒ»¸o¶ÀÁ¢µÄ×÷ÓÃÓo
b. ³¬È«¾Ö±aÁ¿£¬¼È¿ÉÒÔÔÚÈ«¾ÖÒ²¿ÉÔÚ¾Ö²¿Ê¹Ó㬽oÄÜÓÃϵͳ×Ô´øµÄ£¬¾uÊÇÊý×e±aÁ¿¡£
$GLOBALS $_COOKIE $_ENV $_FILES $_GET
$_POST $_REQUEST $_SERVER $_SESSION
c. $GLOBALS
1) ²»ÄÜ´aeÔÚ³¬È«¾Ö±aÁ¿£¬µ«¿ÉÒÔÓг¬È«¾ÖµÄÊý¾Ý£¡
2) ½«ÐeÒªµÄÊý¾Ý*ŵ½³¬È«¾Ö±aÁ¿µÄÊý×eÄÚ£¬µ«Í³Ò»Ê¹ÓÃ$GLOBALS
3) $GLOBALS ÌØÕ÷
- ÿ¸oÈ«¾Ö±aÁ¿¾ÍÊǶÔÓ¦$GLOBALSÄÚµÄÒ»¸oÔªËØ£¡
ÿµ±Ôo¼ÓÒ»¸oÈ«¾Ö£¬Ôo×Ô¶¯ÔÚ$GLOBALSÄÚÔo¼ÓÒ»¸oͬÃuÔªËØ£¡
ͬÀi£¬Ã¿µ±Ôo¼ÓÔªËØ£¬Ò²»aÔo¼ÓÒ»¸oÈ«¾Ö±aÁ¿£¬Ò»°aÔÚº¯ÊýÄÚÔo¼Ó
- ×oµÄÈκÎÐ޸ģ¬Ò²»aÓ³Éaµ½ÁiÒ»¸o£¬°uÀ¨¸uкÍɾ³ý
ÔÚº¯ÊýÄÚ*ÃÎÊÈ«¾Ö±aÁ¿£¬Ö»ÐeʹÓÃ$GLOBALS
- ³oÏÖ¹ýµÄÈ«¾Ö±aÁ¿£¬¾Í¿ÉÒÔͨ¹ý$GLOBALSÕa¸oÊý×eÈ¡µÃ
4) PHPÉuÃuÖÜÆÚÖУ¬¶¨ÒaÔÚº¯ÊýÌaÍa²¿µÄËuνȫ¾Ö±aÁ¿£¬º¯ÊýÄÚ²¿ÊDz»ÄÜÖ±½Ó»ñµÃµÄ
4) global¹Ø¼u×Ö£¨²»½¨ÒeʹÓã(C)
½«¾Ö²¿±aÁ¿ÉuÃ÷ΪͬÃuÈ«¾Ö±aÁ¿µÄÒ»¸o'ÒýÓÃ'£¡Ïaµ±ÓÚ³£Á¿µÄÒýÓô«µÝ
global $var; // $var = &$GLOBALS['var'];
²»Í¬ÓÚ$GLOBALS£¡£¡£¡
globalÔÚº¯Êý²uÉuÒ»¸oÖ¸Ïoº¯ÊýÍa²¿±aÁ¿µÄ±ðÃu±aÁ¿£¬¶ø²»ÊÇÕaeÕýµÄº¯ÊýÍa²¿±aÁ¿¡£
$GLOBALSÈ*È*ʵʵµ÷ÓÃÊÇÍa²¿µÄ±aÁ¿£¬º¯ÊýÄÚÍa»aʼÖÕ±£³ÖÒ»Ö¡£
globalµÄ×÷ÓÃÊǶ¨ÒaÈ«¾Ö±aÁ¿£¬µ«ÊÇÕa¸oÈ«¾Ö±aÁ¿²»ÊÇÓ¦ÓÃÓÚÕu¸oÍøÕ¾£¬¶øÊÇÓ¦ÓÃÓÚµ±Ç°Ò³Ãae£¬°uÀ¨include»orequireµÄËuÓÐÎļþ¡£
d.
1) ×÷ÓÃÓoÖ»Õe¶Ô±aÁ¿£¬¶Ô³£Á¿ÎÞЧ
2) ±»ÔØÈeÎļþÖж¨ÒaµÄ±aÁ¿×÷ÓÃÓoÈ¡¾oÓÚ±»ÔØÈeµÄλÖá£
º¯ÊýÍa±»ÔØÈe¾ÍÊÇÈ«¾Ö£¬º¯ÊýÄÚ±»ÔØÈe¾ÍÊǾֲ¿£¡
/* ¡¾±aÁ¿ÉuÃuÖÜÆÚ¡¿ */
1. ½Å±¾½aÊøʱ£¬È«¾Ö±aÁ¿Ïuʧ
2. º¯ÊýÖ´ÐÐÍeʱ£¬¾Ö²¿±aÁ¿Ïuʧ
3. ¾²Ì¬±aÁ¿
static¹Ø¼u×Ö
¾²Ì¬±aÁ¿½oÔÚ¾Ö²¿º¯ÊýÓoÖдaeÔÚ£¬µ«µ±³ÌÐoÖ´ÐÐÀe¿ª´Ë×÷ÓÃÓoʱ£¬ÆaÖµ²¢²»¶ªÊ§¡£
¾²Ì¬±aÁ¿½o»a±»³oʼ»¯Ò»´Î£¬ÆaËu¾Ö²¿±aÁ¿Ã¿´Î±»µ÷ÓÃʱ¶¼»a±»ÖØи³Öµ¡£
staticÉuÃ÷µÄ¾²Ì¬±aÁ¿µÄÉuÃuÖÜÆÚ»a±»Ò»Ö±ÑÓÐø¡£
/* ¡¾µu´uºÍµÝ¹e¡¿ */
µu´u±ÈµÝ¹eЧÂʸߣ¡
µu´uÊÇÒ»ÖÖ˼Ïe£¨Ëa*¨£(C)£¬½a¹¹ºÍʹÓÃÉÏÈçͬѻ*£¡
µÝ¹eÊÇÒ»ÖÖ˼Ïe£¨Ëa*¨£(C)£¬½«´oÎÊÌa²ð*Ö³ÉСÎÊÌa£¬ÖðÒ»½a¾oСÎÊÌaÒÔ½a¾o´oÎÊÌa
ÒªÇo´oÎÊÌaºÍСÎÊÌaµÄ½a¾o*½°¸ÊÇÒ»Öµģ¡
µÝ¹eµÄ½a¹¹ºÍÓi*¨ÌaÏÖÈçͼº¯Êý¡£º¯ÊýÌaÄÚµ÷Óú¯Êý±¾Éi£¡
µÝ¹e³o¿Ú£ºµ±¸ÃÎÊÌa¿ÉÒÔ½a¾oʱ£¬Ôo²»ÓÃÔٵݹe
/* ¡¾ÄaÃuº¯Êý/±Õ°uº¯Êý¡¿ */
ÄaÃuº¯Êý£¬Ò²½Ð±Õ°uº¯Êý(closures)£¬ÔÊÐiÁÙʱ´´½¨Ò»¸oûÓÐÖ¸¶¨Ãu³ÆµÄº¯Êý¡£
1. ¶¨ÒaÄaÃuº¯Êýʱ£¬²»ÐeÔo¼Óº¯ÊýÃu¡£
2. PHP¶ÔÄaÃuº¯ÊýµÄ¹ÜÀi£¬ÒÔÒ»¸o¶ÔÏoµÄ*½Ê½½øÐд¦Ài¡£
3. ÄaÃuº¯ÊýÓ¦¸Ã´ae*ŵ½±aÁ¿ÄÚ¡£
4. ÄaÃuº¯Êýͨ¹ýClosureÀaÀ´ÊµÏÖ
5. ¿ÉÒÔʹÓú¯Êý×÷Ϊº¯ÊýµÄ²ÎÊýºÍ*µ»ØÖµ
6. ÉuÃ÷º¯Êýʱ¿ÉÒÔʹÓà use($param) À´Ïoº¯ÊýÖд«Èeº¯ÊýÍaµÄ±aÁ¿£¬½aºÏ±aÁ¿ÒýÓÃÀ´ÊµÏÖ±Õ°u
7. ¿ÉÒÔÓñaÁ¿ÒýÓú¯Êý
$func = function ($e) {
echo $e;
}; //½aÊøʱ£¬Ðe*ֺŽaÊø£¬Èçͬ±aÁ¿¸³Öµ
var_dump($func); //ʹÓÃÄaÃuº¯Êý
$func('ITCAST'); //º¯ÊýµÄµ÷ÓÃ
Õa²»Êǿɱaº¯Êý£¬¶øÊǶÔÏo¡£Closure±Õ°uÀa
//useÓi*¨
ÄaÃuº¯ÊýÇaÏoÓÚÖµµÄ¸ÅÄi£¬¿ÉÄܳoÏÖÔÚÈκεØ*½¡£
use¿ÉÒÔʹµÃÄaÃuº¯ÊýʹÓÃÆaÍa²¿×÷ÓÃÓoµÄ±aÁ¿¡£*ÇÈ«¾Ö£¡
useÓeÈ«¾ÖµÄÇø±ð£º
useʹÓÃÆaÍa²¿×÷ÓÃÓoµÄ±aÁ¿
function out() {
$v = "in out";
$func = function () use (& $v) {
var_dump($v);
}
}
useÀaËƲÎÊýµÄ×Ô¶¯´«µÝ£¬Ò²Ö§³ÖÖµÓeÒýÓõĴ«µÝ*½Ê½¡£
//×÷ÓÃ
³£×÷Ϊ'ÁÙʱº¯Êý'±»µ÷Óã¨Ö»ÔÚij¸oµØ*½±»µ÷Óõĺ¯Êý£(C)
ÀýÈ磺
PHP´aeÔÚÒ»¸oarray_map()º¯Êý£¬¹¦ÄÜÊÇÕe¶ÔÒ»¸oº¯ÊýÄÚÿ¸oÔªËØ£¬È¥µ÷ÓÃij¸oº¯Êý
²Ù×÷½a¹u(array) = array_map(²Ù×÷º¯Êý, ²Ù×÷Êý×e);
$result_arr = array_map(function ($v) {return $v3}, $arr);
//±Õ°uÓÃ*¨ÊµÀý
function closureCreater() {
$x = 1;
return function($fun = null) use(&$x) {//°´ÒýÓô«Öµ
echo "<br />" . $x++;
$fun and $fun();
};
}
$x = "hello world";
$test = closureCreater();
$test();
$test(function(){ echo "closure test one"; });
$test(function(){ echo "closure test two"; });
$test(function() use($x){ echo "<br />".$x;});
//½«º¯Êý±£´aeΪÊý×eÔªËØ
$x = 'outer param.';
$arr = array();
$arr[] = function($str)use($x){ return $str.$x; };
echo $arr[0]('test fun in arr,');
/* ¡¾Êý×e¡¿ */
¹ØÁªÊý×e£º¼uºÍÖµÓйØÁª£¬¼u±iʾֵµÄÂß¼º¬Òa¡£
Ë÷ÒýÊý×e£º¼uºÍÖµÎÞ¹ØÁª£¬¼u±iʾֵµÄλÖá£Í¨³£Ï±e´Ó0¿ªÊ¼£¬µÝÔoÔªËØ
count($var [,$mode]) //ͳ¼ÆÊý×eÔªËظoÊý
$mode¿ÉÑ¡£¬ÉeΪ1»otrueʱÔoµÝ¹eͳ¼Æ
$var*ÇÊý×e£¬*µ»Ø1£»$varδ³oʼ»¯»oµÈÓÚnull»o¿ÕÊý×e£¬*µ»Ø0
//¼uÃuµÄʹÓÃ
ÕuÐÍÊý×Ö¼u²»Ðe¼ÓÒýºÅ($arr[1])
×Ö*u´®Êý×Ö¼uÒ²²»Ðe¼ÓÒýºÅ($arr = array('1'=>'abc'); $arr[1])
¹ØÁªÊý×e£¬×Ö*u´®¼uÐe¼ÓÒýºÅ($arr = array('a'=>'aaa'); $arr['a'])
¹ØÁªÊý×e£¬Ë«ÒýºÅÖнaÎo±aÁ¿£¬¿É²»¼ÓÒýºÅ($arr = array('a'=>'aaa'); "$arr[a]")
/* ¡¾Ö¸Õe¡¿ */
current/pos *µ»Øµ±Ç°±»ÄÚ²¿Ö¸ÕeÖ¸ÏoµÄÊý×eµ¥ÔªµÄÖµ£¬²¢²»Òƶ¯Ö¸Õe¡£
key *µ»ØÊý×eÖе±Ç°µ¥ÔªµÄ¼uÃu£¬²¢²»Òƶ¯Ö¸Õe
next ½«Êý×eÖеÄÄÚ²¿Ö¸ÕeÏoÇ°Òƶ¯Ò»Î»£¬²¢*µ»ØÒƶ¯ºoµ±Ç°µ¥ÔªµÄÖµ¡£ÏÈÒƶ¯£¬ÔÙÈ¡Öµ¡£
prev ½«Êý×eµÄÄÚ²¿Ö¸Õeµ¹»Øһ룬²¢*µ»ØÒƶ¯ºoµ±Ç°µ¥ÔªµÄÖµÏÈÒƶ¯£¬ÔÙÈ¡Öµ¡£
end ½«Êý×eµÄÄÚ²¿Ö¸ÕeÖ¸Ïo×iºoÒ»¸oµ¥Ôª£¬²¢*µ»Ø×iºoÒ»¸oµ¥ÔªµÄÖµ
reset ½«Êý×eµÄÄÚ²¿Ö¸ÕeÖ¸ÏoµÚÒ»¸oµ¥Ôª£¬²¢*µ»ØµÚÒ»¸oÊý×eµ¥ÔªµÄÖµ
each *µ»ØÊý×eÖе±Ç°µÄ¼u/Öµ¶Ô²¢½«Êý×eÖ¸ÕeÏoÇ°Òƶ¯Ò»²½¡£
*µ»ØµÄÊÇÒ»¸oÓɼuºÍÖµ×e³ÉµÄ³¤¶ÈΪ4µÄÊý×e£¬Ï±e0ºÍkey±iʾ¼u£¬Ï±e1ºÍvalue±iʾֵ
ÔÚÖ´ÐÐeach()Ö®ºo£¬Êý×eÖ¸Õe½«Í£ÁoÔÚÊý×eÖеÄÏÂÒ»¸oµ¥Ôª
»oÕßµ±Åoµ½Êý×e½aβʱͣÁoÔÚ×iºoÒ»¸oµ¥Ôª¡£
Èç¹uÒªÔÙÓà each ±eÀuÊý×e£¬±ØÐeʹÓà reset()¡£
1. ÒÔÉÏÖ¸Õe²Ù×÷º¯Êý£¬³ýÁËkey()£¬ÈoÖ¸ÕeÒƳoÊý×e£¬Ôo*µ»Øfalse¡£¶økey()ÒƳoÔo*µ»Ønull¡£
2. ÈoÖ¸Õe*Ç*¨£¬²»ÄܽøÐÐnext/prev²Ù×÷£¬ÄܽøÐÐreset/end²Ù×÷
3. current/next/prev ÈoÓoµ½°uº¬¿Õµ¥Ôª£¨0»o""£(C)Ò²»a*µ»Øfalse¡£¶øeach²»»a£¡
list °ÑÊý×eÖеÄÖµ¸³¸øÒ»Ð(C)±aÁ¿¡£list()ÊÇÓiÑÔ½a¹¹£¬²»ÊǺ¯Êý
½oÄÜÓÃÓÚÊý×ÖË÷ÒýµÄÊý×e²¢¼Ù¶¨Êý×ÖË÷Òý´Ó0¿ªÊ¼
/* ¿ÉÓÃÓÚ½»»»¶a¸o±aÁ¿µÄÖµ */ list($a, $b) = array($b, $a);
Àý£ºlist($drink, , $power) = array('coffee', 'brown', 'caffeine');
1. ¸´ÖÆÊý×e£¬ÆaÖ¸ÕeλÖÃÒ²»a±»¸´ÖÆ¡£
ÌØÀý£ºÈç¹uÊý×eÖ¸Õe*Ç*¨£¬Ôo¿½±´µÄÊý×eÖ¸Õe»aÖØÖ㬶øÔÊý×eµÄÖ¸Õe²»±a¡£
¡¾Ö¸ÕeÎÊÌa¡¿
˵ÚÒ»¸o½øÐÐд²Ù×÷£¬¾Í»a¿ª±ÙÒ»¸oеÄÖµ¿Õ¼a¡£Óe±aÁ¿(Êý×e±aÁ¿)Öµ´«µÝ¸øËÎ޹ء£
Êý×eº¯Êýcurrent()±»¶¨ÒaΪд²Ù×÷£¬¹Ê»a³oÏÖÎÊÌa¡£
foreach±eÀuµÄÊÇÊý×eµÄ¿½±´£¬µ±±»Ð´Ê±£¬²Å»a¿ª±ÙÒ»¸oеÄÖµ¿Õ¼a¡£
¼´£¬foreachÑ»*Ìa¶ÔÔÊý×e½øÐÐд²Ù×÷ʱ£¬²Å»a³oÏÖÖ¸ÕeÎÊÌa¡£
Èç¹u¿ª±ÙпռaʱָÕe*Ç*¨£¬Ôo»a³oʼ»¯Ö¸Õe¡£
2. Èç¹uÖ¸ÕeλÖóoÏÖÎÊÌa£¬Ôoreset()³oʼ»¯Ò»Ï¾Ϳɽa¾o¡£
/* ¡¾±eÀuÊý×e¡¿ */
* ÏÈÕÒµ½ÔªËØ£¬ÔÙ»ñÈ¡¼uºÍÖµ
foreach
foreach (array_expression as [$key =>] & $value)
µ±foreach¿ªÊ¼Ö´ÐÐʱ£¬Êý×eÄÚ²¿µÄÖ¸Õe»a×Ô¶¯Ö¸ÏoµÚÒ»¸oµ¥Ôª¡£
»ñÈ¡ÔªËØÐÅÏ¢ºo£¬Òƶ¯Ö¸Õe£¬ÔÙÖ´ÐÐÑ»*Ìa
1. foreach±¾ÉiÑ»*½a¹¹£¬breakºÍcontinueÊÊÓÃÓÚforeach
2. foreachÖ§³ÖÑ»*µÄÌae´uÓi*¨¡£
3. $valueÊDZ£´aeÔªËØÖµµÄ±aÁ¿£¬¶ÔÆaÐ޸IJ»»a¸Ä±aÊý×eµÄÔªËØÖµ
4. $valueÖ§³ÖÔªËØÖµµÄÒýÓÿ½±´£¬ÔÚ$valueÇ°¼ÓÉÏ&¼´¿É
5. $key²»Ö§³ÖÒýÓô«µÝ
6. foreach±eÀuµÄÊÇÔÊý×eµÄ¿½±´£¬¶øÔÚÑ»*Ìa¶ÔÊý×eµÄ²Ù×÷ÊDzÙ×÷ÔÊý×e
¼´Ñ»*Ìa¶ÔÊý×eµÄ²Ù×÷£¬¶ÔÔÊý×eÉuЧ£¬¶Ô±eÀu²»ÉuЧ¡£
ÏÈ¿½±´Ò»*ÝÊý×eÓÃ×÷±eÀu
while...list...each
while (list($key, $val) = mysql_fetch_row($result)) = each($arr) {
echo "$key => $val\n";
}
/* ¡¾Êý×eº¯Êý¡¿ */
//ͳ¼Æ¼ÆËa
count ¼ÆËaÊý×eÖеĵ¥ÔªÊýÄ¿»o¶ÔÏoÖеÄÊoÐÔ¸oÊý
array_count_values ͳ¼ÆÊý×eÖÐËuÓеÄÖµ³oÏֵĴÎÊý
array_product ¼ÆËaÊý×eÖÐËuÓÐÖµµÄ³Ë»ý
array_sum ¼ÆËaÊý×eÖÐËuÓÐÖµµÄºÍ
range ½¨Á¢Ò»¸o°uº¬Ö¸¶¨*¶Î§µ¥ÔªµÄÊý×e
//»ñÈ¡Êý×eÄÚÈÝ
array_chunk ½«Ò»¸oÊý×e*Ö¸i³É¶a¸o
array array_chunk(array $input, int $size[, bool $preserve_keys])
array_filter Óûص÷º¯Êý¹ýÂËÊý×eÖеĵ¥Ôª
array_slice ´ÓÊý×eÖÐÈ¡³oÒ»¶Î
array array_slice($arr, $offset [,$len [,$preserve_keys]])
array_keys *µ»ØÊý×eÖÐËuÓеļuÃu
array array_keys(array $input[, mixed $search_value[, bool $strict]] )
Èç¹uÖ¸¶¨ÁË¿ÉÑ¡²ÎÊý search_value£¬ÔoÖ»*µ»Ø¸ÃÖµµÄ¼uÃu¡£*ñÔoinputÊý×eÖеÄËuÓмuÃu¶¼»a±»*µ»Ø¡£
array_values *µ»ØÊý×eÖÐËuÓеÄÖµ£¬²¢½¨Á¢Êý×ÖË÷Òý
array_merge ºÏ²¢Ò»¸o»o¶a¸oÊý×e
Ò»¸oÊý×eÖеÄÖµ¸½¼ÓÔÚÇ°Ò»¸oÊý×eµÄºoÃae¡£
Èç¹uÊaÈeµÄÊý×eÖÐÓÐÏaͬµÄ×Ö*u´®¼uÃu£¬Ôo¸Ã¼uÃuºoÃaeµÄÖµ½«¸²¸ÇÇ°Ò»¸oÖµ¡£
Èç¹uÊý×e°uº¬Êý×Ö¼uÃu£¬ºoÃaeµÄÖµ½«²»»a¸²¸ÇÔÀ´µÄÖµ£¬¶øÊǸ½¼Óµ½ºoÃae¡£
Èç¹uÖ»¸øÁËÒ»¸oÊý×e²¢ÇÒ¸ÃÊý×eÊÇÊý×ÖË÷ÒýµÄ£¬Ôo¼uÃu»aÒÔÁ¬Ðø*½Ê½ÖØÐÂË÷Òý¡£
array_merge_recursive µÝ¹eµØºÏ²¢Ò»¸o»o¶a¸oÊý×e
//ËÑË÷
in_array ¼i²eÊý×eÖÐÊÇ*ñ´aeÔÚij¸oÖµ
bool in_array(mixed $needle, array $haystack[, bool $strict])
array_key_exists ¼i²e¸ø¶¨µÄ¼uÃu»oË÷ÒýÊÇ*ñ´aeÔÚÓÚÊý×eÖÐ
isset()¶ÔÓÚÊý×eÖÐΪNULLµÄÖµ²»»a*µ»ØTRUE£¬¶ø array_key_exists()»a
array_search ÔÚÊý×eÖÐËÑË÷¸ø¶¨µÄÖµ£¬Èç¹u³É¹¦Ôo*µ»ØÏaÓ¦µÄ¼uÃu
array_combine ´´½¨Ò»¸oÊý×e£¬ÓÃÒ»¸oÊý×eµÄÖµ×÷ΪÆa¼uÃu£¬ÁiÒ»¸oÊý×eµÄÖµ×÷ΪÆaÖµ
Èç¹uÁ½¸oÊý×eµÄµ¥ÔªÊý²»Í¬»oÕßÊý×eΪ¿Õʱ*µ»ØFALSE¡£
array_rand ´ÓÊý×eÖÐËae»uÈ¡³oÒ»¸o»o¶a¸oµ¥Ôª£¬*µ»Ø¼uÃu»o¼uÃu×e³ÉµÄÊý×e£¬Ï±eÊÇ×ÔÈ»ÅÅÐoµÄ
array_fill Óøø¶¨µÄÖµÌi³aÊý×e
array_fill($start, $num, $value)
array_flip ½»»»Êý×eÖеļuºÍÖµ
array_pad ÓÃÖµ½«Êý×eÌi²¹µ½Ö¸¶¨³¤¶È
array_reverse *µ»ØÒ»¸oµ¥ÔªË³ÐoÏa*´µÄÊý×e
array_unique ÒƳýÊý×eÖÐÖظ´µÄÖµ
array_splice °ÑÊý×eÖеÄÒ»²¿*ÖÈ¥µo²¢ÓÃÆaËuֵȡ´u
implode ½«Êý×eÔªËØÖµÓÃij¸o×Ö*u´®Á¬½Ó³É×Ö*u´®
explode($delimiter, $str [,$limit]) //ʹÓÃÒ»¸o×Ö*u´®*Ö¸iÁiÒ»¸o×Ö*u´®
$delimiter²»ÄÜΪ¿Õ×Ö*u´®""
array_map ½«»Øµ÷º¯Êý×÷Óõ½¸ø¶¨Êý×eµÄµ¥ÔªÉÏ£¬Ö»ÄÜ´¦ÀiÔªËØÖµ£¬¿ÉÒÔ´¦Ài¶a¸oÊý×e
Èç¹ucallback²ÎÊýÉeΪnull£¬ÔoºÏ²¢¶a¸oÊý×eΪһ¸o¶aάÊý×e
array_walk ¶ÔÊý×eÖеÄÿ¸o³ÉÔ±Ó¦ÓÃÓû§º¯Êý£¬Ö»ÄÜ´¦ÀiÒ»¸oÊý×e£¬¼uºÍÖµ¾u¿É´¦Ài£¬Óeforeach¹¦ÄÜÏaͬ
bool array_walk ( array &$array , callback $funcname [, mixed $userdata ] )
//Õ»£ººo½øÏȳo
ÈeÕ»ºÍ³oÕ»»aÖØÐÂ*ÖÅaË÷Òýϱe
array_push ½«Ò»¸o»o¶a¸oµ¥ÔªÑ¹ÈeÊý×eµÄÄ(C)⣨ÈeÕ»£(C)
array_pop ½«Êý×e×iºoÒ»¸oµ¥Ôªµ¯³o£¨³oÕ»£(C) ʹÓô˺¯Êýºo»aÖØÖÃ(reset())array Ö¸Õe¡£
//¶ÓÁУºÏȽøÏȳo
¶ÓÁк¯Êý»aÖØÐÂ*ÖÅaË÷Òýϱe
array_unshift ÔÚÊý×e¿ªÍ*²aÈeÒ»¸o»o¶a¸oµ¥Ôª
array_shift ½«Êý×e¿ªÍ*µÄµ¥ÔªÒƳoÊý×e ʹÓô˺¯Êýºo»aÖØÖÃ(reset())array Ö¸Õe¡£
//ÅÅÐoº¯Êý
sort ¶ÔÊý×eÅÅÐo
rsort ¶ÔÊý×eÄaeÏoÅÅÐo
asort ¶ÔÊý×e½øÐÐÅÅÐo²¢±£³ÖË÷Òý¹Øϵ
arsort ¶ÔÊý×e½øÐÐÄaeÏoÅÅÐo²¢±£³ÖË÷Òý¹Øϵ
ksort ¶ÔÊý×e°´ÕÕ¼uÃuÅÅÐo
krsort ¶ÔÊý×e°´ÕÕ¼uÃuÄaeÏoÅÅÐo
usort ʹÓÃÓû§×Ô¶¨ÒaµÄ±È½Ïº¯Êý¶ÔÊý×eÖеÄÖµ½øÐÐÅÅÐo
uksort ʹÓÃÓû§×Ô¶¨ÒaµÄ±È½Ïº¯Êý¶ÔÊý×eÖеļuÃu½øÐÐÅÅÐo
uasort ʹÓÃÓû§×Ô¶¨ÒaµÄ±È½Ïº¯Êý¶ÔÊý×eÖеÄÖµ½øÐÐÅÅÐo²¢±£³ÖË÷Òý¹ØÁª
natsort ÓÃÓá°×ÔÈ»ÅÅÐo¡±Ëa*¨¶ÔÊý×eÅÅÐo
natcasesort Óá°×ÔÈ»ÅÅÐo¡±Ëa*¨¶ÔÊý×e½øÐв»Çø*Ö´oСд×ÖĸµÄÅÅÐo
array_multisort ¶Ô¶a¸oÊý×e»o¶aάÊý×e½øÐÐÅÅÐo
shuffle ½«Êý×e´oÂÒ
ÒýÓô«µÝ²ÎÊý£¬*µ»ØboolÖµ¡£
ÖØи³ÓeË÷Òý¼uÃu£¬É¾³ýÔÓмuÃu
//²i¼¯
array_udiff_assoc ´øË÷Òý¼i²e¼ÆËaÊý×eµÄ²i¼¯£¬Óûص÷º¯Êý±È½ÏÊý¾Ý
array_udiff_uassoc ´øË÷Òý¼i²e¼ÆËaÊý×eµÄ²i¼¯£¬Óûص÷º¯Êý±È½ÏÊý¾ÝºÍË÷Òý
array_udiff Óûص÷º¯Êý±È½ÏÊý¾ÝÀ´¼ÆËaÊý×eµÄ²i¼¯
array_diff_assoc ´øË÷Òý¼i²e¼ÆËaÊý×eµÄ²i¼¯
array_diff_key ʹÓüuÃu±È½Ï¼ÆËaÊý×eµÄ²i¼¯
array_diff_uassoc ÓÃÓû§Ìa¹(C)µÄ»Øµ÷º¯Êý×oË÷Òý¼i²eÀ´¼ÆËaÊý×eµÄ²i¼¯
array_diff_ukey Óûص÷º¯Êý¶Ô¼uÃu±È½Ï¼ÆËaÊý×eµÄ²i¼¯
array_diff ¼ÆËaÊý×eµÄ²i¼¯
//½»¼¯
array_intersect_assoc ´øË÷Òý¼i²e¼ÆËaÊý×eµÄ½»¼¯
array_intersect_key ʹÓüuÃu±È½Ï¼ÆËaÊý×eµÄ½»¼¯
array_intersect_uassoc ´øË÷Òý¼i²e¼ÆËaÊý×eµÄ½»¼¯£¬Óûص÷º¯Êý±È½ÏË÷Òý
array_intersect_ukey Óûص÷º¯Êý±È½Ï¼uÃuÀ´¼ÆËaÊý×eµÄ½»¼¯
array_intersect ¼ÆËaÊý×eµÄ½»¼¯
array_key_exists Óûص÷º¯Êý±È½Ï¼uÃuÀ´¼ÆËaÊý×eµÄ½»¼¯
array_uintersect_assoc ´øË÷Òý¼i²e¼ÆËaÊý×eµÄ½»¼¯£¬Óûص÷º¯Êý±È½ÏÊý¾Ý
array_uintersect ¼ÆËaÊý×eµÄ½»¼¯£¬Óûص÷º¯Êý±È½ÏÊý¾Ý
extract($arr [,$type [,$prefix]]) ´ÓÊý×eÖн«±aÁ¿µ¼Èeµ½µ±Ç°µÄ*uºÅ±i(½ÓÊܽaºÏÊý×e$arr×÷Ϊ²ÎÊý²¢½«¼uÃuµ±×÷±aÁ¿Ãu£¬Öµ×÷Ϊ±aÁ¿µÄÖµ)
compact($var [,...]) ½¨Á¢Ò»¸oÊý×e£¬°uÀ¨±aÁ¿ÃuºÍËuÃǵÄÖµ(±aÁ¿Ãu³ÉΪ¼uÃu¶ø±aÁ¿µÄÄÚÈݳÉΪ¸Ã¼uµÄÖµ)
/* ¡¾Î±ÀaÐÍ¡¿ */
mixed ˵Ã÷Ò»¸o²ÎÊý¿ÉÒÔ½ÓÊܶaÖÖ²»Í¬µÄ£¨µ«²¢²»±ØÐeÊÇËuÓеģ(C)ÀaÐÍ¡£
number ˵Ã÷Ò»¸o²ÎÊý¿ÉÒÔÊÇ integer »oÕß float¡£
callback »Øµ÷º¯Êý
void void×÷Ϊ*µ»ØÀaÐÍÒaζן¯ÊýµÄ*µ»ØÖµÊÇÎÞÓõġ£
void×÷Ϊ²ÎÊýÁбiÒaζן¯Êý²»½ÓÊÜÈκβÎÊý¡£
/* ¡¾Êý¾Ý¿a²Ù×÷¡¿ */
#Á¬½ÓÈÏÖ¤
mysql_connect Á¬½Ó²¢ÈÏÖ¤Êý¾Ý¿a
#*¢ËÍSQLÓi¾a£¬½ÓÊÕÖ´Ðнa¹u
mysql_query *¢ËÍSQLÓi¾a
½o¶Ôselect, show, explain, describeÓi¾aÖ´Ðгɹ¦*µ»ØÒ»¸o×ÊÔ´±eʶ*u£¬ÆaËuÓi¾a³É¹¦*µ»Øtrue¡£Ö´ÐÐʧ°Ü¾u*µ»Øfalse¡£
#´¦Ài½a¹u
mysql_fetch_assoc ´Ó½a¹u¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊ¹ØÁªÊý×e
ÿ´Îֻȡ»ØÒ»Ìo£¬ÀaËÆeach
½a¹u¼¯ÖмǼָÕe
mysql_fetch_row ´Ó½a¹u¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊö¾ÙÊý×e
mysql_fetch_array ´Ó½a¹u¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊ¹ØÁªÊý×e£¬»oÊý×ÖÊý×e£¬»o¶þÕß¼aeÓÐ
array mysql_fetch_array ( resource $result [, int $ result_type ] )
¿ÉÑ¡²ÎÊýresult_type¿ÉѡֵΪ£ºMYSQL_ASSOC£¬MYSQL_NUM ºÍ MYSQL_BOTH(ĬÈÏ)
mysql_free_result ÊÍ*Ža¹uÄÚ´ae
#¹Ø±ÕÁ´½Ó
mysql_close ¹Ø±ÕÁ¬½Ó
/* ¡¾ÀaºÍ¶ÔÏo¡¿ */
# ³ÉÔ±£º
Àa³ÉÔ±£ºÀa³£Á¿¡¢¾²Ì¬ÊoÐÔ¡¢¾²Ì¬*½*¨
¶ÔÏo³ÉÔ±£º*Ǿ²Ì¬ÊoÐÔ¡¢*Ǿ²Ì¬*½*¨
# ³ý´ËÍa£¬Àa²»ÄÜ°uº¬ÈκÎÆaËu¶«Î÷£¡£¡£¡
# ÀaÃu¡¢*½*¨Ãu¡¢ÊoÐÔÃu¾u²»Çø*Ö´oСд
# $this´u±i±¾¶ÔÏo£¬self´u±i±¾Àa£¬parent´u±i¸¸Àa
# ÀaºÍº¯Êý¾u¿É±»ÊÂÏȱaÒe£¨½o×÷Ϊ×iÍa²aʱ£(C)
# ÀaµÄ¶¨Òa±ØÐeÔÚµ¥Ò»µÄPHPÇø¿eÄÚ£¬²»Äܱ»¶a¸oPHP±eÇ(C)*Ö¸i
// ¹¹Ôi*½*¨
- ¾ßÓй¹Ôiº¯ÊýµÄÀa»aÔÚÿ´Î´´½¨Ð¶ÔÏoʱÏȵ÷ÓôË*½*¨
void __construct([ mixed $args [, $... ]] )
- ¹¹Ôi*½*¨ËuÐe²ÎÊýÓÉnewʵÀý»¯¶ÔÏoʱ£¬¸øÀaÔo¼Ó²ÎÊýÖµ¡£
- ¹¹Ôi*½*¨Ò²¿ÉÒÔ±»ÊÖ¶¯µ÷Óá£
- 5.3.3°ae±¾ÒÔÇ°£¬Ö§³ÖÓÚÀaÃuͬÃuµÄ*½*¨×÷Ϊ¹¹Ôi*½*¨¡£
- Á½ÖÖ³aͻʱ£¬__construct ÓÅÏÈ
// Îo¹¹*½*¨
- Îo¹¹º¯Êý»aÔÚµ½Ä³¸o¶ÔÏoµÄËuÓÐÒýÓö¼±»É¾³ý»oÕßµ±¶ÔÏo±»ÏÔʽÏu»ÙʱִÐС£
void __destruct( void )
# ×÷ÓãºÊÍ*ŶÔÏoËuÕ¼ÓõÄ×ÊÔ´
# µ÷ÓõÄʱ»u
- ½Å±¾½aÊøʱËuÓÐ×ÊÔ´¾u±»ÊÍ*Å£¬°uÀ¨¶ÔÏo
- ÊÖ¶¯É¾³ý¶ÔÏoʱ
- ±£´ae¶ÔÏoµÄ±aÁ¿±»¸³ÓeÐÂֵʱ(ÈκÎÖµ£¬°uÀ¨null)
- ÔÚʹÓÃexit()ÖÕÖ¹½Å±¾ÔËÐÐʱҲ»a±»µ÷ÓÃ
// ¾²Ì¬³ÉÔ±(static¹Ø¼u×Ö)
- ÉuÃ÷Àa³ÉÔ±»o*½*¨Îªstatic£¬¾Í¿ÉÒÔ²»ÊµÀý»¯Àa¶øÖ±½Ó*ÃÎÊ¡£
- ¾²Ì¬³ÉÔ±£¨ÊoÐÔ»o*½*¨£(C)¾uÊoÓÚÀa£¬¹Ê²»ÄÜͨ¹ý$this»o->*ÃÎÊ¡£
- ¾²Ì¬³ÉÔ±ÊÇËuÓжÔÏo¹²Ïi£¬ÊoÓÚÀa¡£
- ¾²Ì¬³ÉÔ±ÓÃÀaµ÷Óã¬*Ǿ²Ì¬³ÉÔ±ÓöÔÏoµ÷Óá£
# ¾²Ì¬ÊoÐÔ
- ¾²Ì¬ÊoÐÔ²»¿ÉÒÔÓɶÔÏoͨ¹ý->²Ù×÷*uÀ´*ÃÎÊ¡£
- ¾²Ì¬ÊoÐÔÖ»Äܱ»³oʼ»¯ÎªÒ»¸o×Ö*uÖµ»oÒ»¸o³£Á¿£¬²»ÄÜʹÓñi´iʽ¡£ ËuÒÔÄa¿ÉÒ԰Ѿ²Ì¬ÊoÐÔ³oʼ»¯ÎªÕuÐÍ»oÊý×e£¬µ«²»ÄÜÖ¸ÏoÁiÒ»¸o±aÁ¿»oº¯Êý*µ»ØÖµ£¬Ò²²»ÄÜÖ¸ÏoÒ»¸o¶ÔÏo¡£
# ¾²Ì¬*½*¨
- ÓÉÓÚ¾²Ì¬*½*¨²»ÐeҪͨ¹ý¶ÔÏo¼´¿Éµ÷Óã¬ËuÒÔα±aÁ¿$thisÔÚ¾²Ì¬*½*¨Öв»¿ÉÓá£
- ÓÃ::*½Ê½µ÷ÓÃÒ»¸o*Ǿ²Ì¬*½*¨»aµ¼ÖÂÒ»¸oE_STRICT¼¶±ðµÄ´iÎo¡£
// *ÃÎʽaÎo²Ù×÷*u(::)
- ¿ÉÒÔÓÃÓÚ*ÃÎʾ²Ì¬³ÉÔ±¡¢*½*¨ºÍ³£Á¿£¬»¹¿ÉÒÔÓÃÓÚ¸²¸ÇÀaÖеijÉÔ±ºÍ*½*¨¡£
- µ±ÔÚÀaµÄÍa²¿*ÃÎÊÕaÐ(C)¾²Ì¬³ÉÔ±¡¢*½*¨ºÍ³£Á¿Ê±£¬±ØÐeʹÓÃÀaµÄÃu×Ö¡£
- self ºÍ parent ÕaÁ½¸oÌØÊaµÄ¹Ø¼u×ÖÊÇÓÃÓÚÔÚÀaµÄÄÚ²¿¶Ô³ÉÔ±»o*½*¨½øÐÐ*ÃÎʵġ£
// *ÃÎʱaeÎo
- ¶ÔÏo³ÉÔ±£¬ÄÚ²¿Í¨¹ý$thisÖ¸¶¨£¬Ía²¿Í¨¹ý¶ÔÏoÃuÖ¸¶¨£¬¾uÓÃ->*ÃÎÊ£¬*ÃÎÊÊoÐÔʱ²»Ðe¼Ó$¡£
¶ÔÏoÃu->ÊoÐÔÃu ¶ÔÏoÃu->*½*¨Ãu() $this->ÊoÐÔÃu $this->*½*¨Ãu()
- Àa³ÉÔ±£¬ÄÚ²¿Í¨¹ýself»oparentÖ¸¶¨£¬Ía²¿Í¨¹ýÀaÃuÖ¸¶¨£¬¾uÓÃ::*ÃÎÊ£¬*ÃÎÊÊoÐÔʱÐe¼Ó$¡£
ÀaÃu::$ÊoÐÔÃu ÀaÃu::*½*¨Ãu() self::$ÊoÐÔÃu self::*½*¨Ãu()
- ÌØÊa£ºÒ²¿ÉÒÔͨ¹ý¶ÔÏo*ÃÎÊÀa³ÉÔ±¡££¨²»½¨Òe£(C)
¶ÔÏoÃu::$ÀaÊoÐÔÃu $this::$ÀaÊoÐÔÃu ¶ÔÏoÃu::$Àa*½*¨Ãu() $this::Àa*½*¨Ãu()
# ¶ÔÏo³ÉÔ±*ÃÎÊÓÃ->£¬Àa³ÉÔ±*ÃÎÊÓÃ::
- ÎÞÂÛÊǾ²Ì¬*½*¨»¹ÊÇ*Ǿ²Ì¬*½*¨£¬¾u¿Éͨ¹ýÀa»o¶ÔÏo½øÐÐ*ÃÎÊ¡£
- ¾²Ì¬ÊoÐÔͨ¹ýÀa*ÃÎÊ£¬¾²Ì¬*½*¨Í¨¹ý¶ÔÏo*ÃÎÊ¡£
- Ö»ÓÐʹÓöÔÏoµ÷ÓÃ*Ǿ²Ì¬*½*¨Ê±£¬$this²Å¿ÉÒÔʹÓã¡
- ¾²Ì¬*½*¨²»¿ÉʹÓÃ$this¡£
- Àa¿ÉÒÔµ÷ÓöÔÏo*½*¨£¬µ«×¢Òa*½*¨ÄÚ²»ÄÜÓÐ$this¡£
- *Ǿ²Ì¬*½*¨¿ÉÒÔµ÷Óþ²Ì¬ÊoÐÔ»o¾²Ì¬*½*¨£¬*´Ö®²»¿ÉÒÔ¡£
// Àa³£Á¿
- ³£Á¿µÄÖµ½«Ê¼ÖÕ±£³Ö²»±a¡£
- ÔÚ¶¨ÒaºÍʹÓó£Á¿µÄʱºo²»ÐeҪʹÓÃ$*uºÅ¡£
- ³£Á¿µÄÖµ±ØÐeÊÇÒ»¸o¶¨Öµ£¬²»ÄÜÊDZaÁ¿£¬ÀaÊoÐÔ»oÆaËu²Ù×÷£¨È纯Êýµ÷Óã(C)µÄ½a¹u¡£
# ¶¨Òa£ºconst ³£Á¿Ãu = ³£Á¿Öµ;
- ²»ÐeÒª¼ÓpublicµÈ*ÃÎÊÐÞÊÎÏÞ¶¨*u
- Àa³£Á¿ÊoÓÚÀa£¬Ê¹ÓÃÀa*ÃÎÊ£¬ÀaÃu::Àa³£Á¿ »o self::Àa³£Á¿
// ×Ô¶¯¼ÓÔضÔÏo
- ÔÚÊÔͼʹÓÃÉÐδ±»¶¨ÒaµÄÀaʱ×Ô¶¯µ÷Óà __autoload º¯Êý
- ×Ô¶¯¼ÓÔØʹÓõ½µÄÀaÃuÎļþ£¨¸u¾ÝÀaÃuÕÒÏaÓ¦Ãu³ÆµÄÎļþ£¬¹ÊÐeÀaÃuÓeÀaÎļþÃuÒ»Ö£(C)
- ÿ¸oÐeÒª¼ÓÔØÀaµÄÎļþ¶¼ÐeÒª´aeÔÚ__autoloadº¯Êý
- ½«__autoloadº¯ÊýдÈeµ¥¶ÀµÄÎļþ£¬Ã¿¸oÐeÒªÓõ½ÀaµÄÎļþÔÙrequire¸Ãº¯ÊýÎļþ
- __autoload ²ÎÊýÊÇÀaÃu
function __autoload($class_name) {
require_once $_SERVER["DOCUMENT_ROOT"] . "/class/$class_name.php";
}
// $_SERVER["DOCUMENT_ROOT"] µ±Ç°ÔËÐнű¾ËuÔÚµÄÎĵµ¸uĿ¼
- ¿ÉÒÔͨ¹ýÀaÃu£¬À´ÍƵ¼³oÀaËuÔÚµÄÎļþÃu£¡
- Èç¹uÒ»¸oÏiÄ¿´aeÔÚ¶a¸o×Ô¶¯¼ÓÔغ¯Êýʱ£¬¶¨ÒaÒ»¸o¿ÉÒÔÍe³É¼ÓÔصÄÆÕͨº¯Êý£¬²¢ÔÚº¯Êý֮ǰʹÓÃspl_autoload_register×¢²a¸Ãº¯Êý¡£
# spl_autoload_register
- ×¢²a__autoload()º¯Êý
bool spl_autoload_register ([ callback $autoload_function ] )
- ¿ÉÒÔ×¢²a¶a¸o×Ô¶¯¼ÓÔغ¯Êý£¬ÏÈ×¢²aµÄÏÈÖ´ÐÐ
- Ò»µ(C)×¢²a×Ô¶¯¼ÓÔغ¯Êý£¬__autoload¾ÍʧЧ¡£
- ×¢²aº¯Êýʱ£¬²ÎÊýΪº¯ÊýÃu£¨×¢Òa¼ÓÒýºÅ£(C)£»×¢²a*½*¨Ê±£¬²ÎÊýΪÊý×e
# ×¢²aÀa»o¶ÔÏoµÄ*½*¨Îª×Ô¶¯¼ÓÔØ*½*¨Ê±£¬²ÎÊýÐeΪÊý×e£º
spl_autoload_register(array(__CLASS__, '__autoload'));
__CLASS__±iʾµ±Ç°ÀaÃu£¬ÈoÊǶÔÏo¿ÉÓÃ$this£¬Ïeϸ¼uÊÖ²a
// ÐoÁл¯£¨´®Ðл¯£(C)
# Êý¾Ý´«Êa¾uÊÇ×Ö*u´®ÀaÐÍ
# ³ýÁË×ÊÔ´ÀaÐÍ£¬¾u¿ÉÐoÁл¯
# ÐoÁл¯ÔÚ´ae*ÅÊý¾Ýʱ£¬»a´ae*ÅÊý¾Ý±¾Éi£¬Ò²»a´ae*ÅÊý¾ÝÀaÐÍ
×÷Óãº1.ÔÚÍøÂç´«ÊaÊý¾Ýʱ£»2.ΪÁ˽«Êý×e»o¶ÔÏo*ÅÔÚ´ÅÅÌʱ
# ÐoÁл¯
serialize ²uÉuÒ»¸o¿É´ae´¢µÄÖµµÄ±iʾ
string serialize ( mixed $value )
- *µ»Ø×Ö*u´®£¬´Ë×Ö*u´®°uº¬Á˱iʾvalueµÄ×Ö½ÚÁ÷£¬¿ÉÒÔ´ae´¢ÓÚÈκεØ*½¡£
- ÓÐÀuÓÚ´ae´¢»o´«µÝ PHP µÄÖµ£¬Í¬Ê±²»¶ªÊ§ÆaÀaÐͺͽa¹¹¡£
# *´ÐoÁл¯
unserialize ´ÓÒÑ´ae´¢µÄ±iʾÖд´½¨PHPµÄÖµ
mixed unserialize ( string $str [, string $callback ] )
- ¶Ôµ¥Ò»µÄÒÑÐoÁл¯µÄ±aÁ¿½øÐвÙ×÷£¬½«Æaת»»»ØPHPµÄÖµ¡£
# ÎļþµÄ¶Áд²Ù×÷
- file_put_contents ½«Ò»¸o×Ö*u´®Ð´ÈeÎļþ
int file_put_contents($file, $data [,$flags])
$flags£ºFILE_USE_INCLUDE_PATH(¸²¸Ç)£¬FILE_APPEND(×*¼Ó)
- file_get_contents ½«Õu¸oÎļþ¶ÁÈeÒ»¸o×Ö*u´®
string file_get_contents($file [, bool $use_include_path [,int $offset [,int $maxlen]]])
# ¶ÔÏoÐoÁл¯
- Ö»ÄÜÐoÁл¯¶ÔÏoÄÚ²¿µÄÊý¾Ý£¬¼´*Ǿ²Ì¬ÊoÐÔ¡£
# ÐeÔÚ*´ÐoÁл¯¶ÔÏo֮ǰ¼ÓÔØÀa£¬Ò²¿ÉÒÔ´¥*¢×Ô¶¯¼ÓÔØ»uÖÆ¡£
__sleep ÐoÁл¯ÐeÐoÁл¯µÄÊoÐÔ¡£
- Ìa½»Î´Ìa½»µÄÊý¾Ý£¬»oÀaËƵÄÇaÀi²Ù×÷£¬²¿*Ö´®Ðл¯¶ÔÏo¡£
- *µ»ØÒ»¸o°uº¬¶ÔÏoÖÐËuÓÐÓ¦±»ÐoÁл¯µÄ±aÁ¿Ãu³ÆµÄÊý×e
__wakeup *´ÐoÁл¯Ê±£¬Ô¤ÏÈ×¼±¸¶ÔÏoÐeÒªµÄ×ÊÔ´
- ÖØн¨Á¢Êý¾Ý¿aÁ¬½Ó£¬»oÖ´ÐÐÆaËu³oʼ»¯²Ù×÷
public function __sleep() {
return array('server', 'username', 'password', 'db');
}
public function __wakeup() {
$this->connect();
}
// ¶ÔÏo¼Ì³Ð
class ×ÓÀaÃu extends ¸¸Àa {}
Èç¹uÒ»¸o¶ÔÏoÊÇ×ÓÀaµÄ¶ÔÏo£¬ÄÇôͬʱҲÊǸ¸ÀaµÄ¶ÔÏo¡£
µ¥¼Ì³Ð£ºÒ»¸oÀaÖ»Äܼ̳ÐÒ»¸o¸¸Àa£¬²»ÄÜͬʱ¼Ì³Ð¶a¸oÀa¡£µ«Ò»¸o¸¸Àa¿ÉÒÔ±»¶a¸o×ÓÀa¼Ì³Ð¡£
instanceof ÅжÏij¶ÔÏoÊÇ*ñΪijÀaµÄ¶ÔÏo
¶ÔÏoÃu instanceof ÀaÃu
// *ÃÎÊ¿ØÖÆ
public ¹«Óеģ¨¼Ì³ÐÁ´¡¢±¾Àa¡¢Ía²¿¾u¿É*ÃÎÊ£(C)
protected ±£»¤µÄ£¨½o¼Ì³ÐÁ´¡¢±¾Àa¿É*ÃÎÊ£(C)
private ˽Óеģ¨½o±¾Àa¿É*ÃÎÊ£(C)
¸u¾Ý³ÉÔ±¶¨ÒaλÖá¢*ÃÎÊλÖÃÅжϡ£
# ¼aeÈÝÐÔÎÊÌa
- ÉuÃ÷ÊoÐÔʱ£¬var¹Ø¼u×ÖÉuÃ÷µÄĬÈÏΪpublicȨÏÞ
- ÉuÃ÷*½*¨Ê±£¬Ê¡ÂÔ*ÃÎÊÐÞÊÎ*u£¬Ä¬ÈÏΪpublicȨÏÞ
// ÖØд override
$this´u±i±¾¶ÔÏo£¬±»Ëµ÷Ó㬾ʹu±iÄĸo¶ÔÏo¡£
- ¼Ì³Ðʱ£¬×ÓÀa³ÉÔ±ÃuÓÚ¸¸Àa³ÉÔ±Ãu*¢Éu³aÍ»£¬Ôo×ÓÀa³ÉÔ±»aÖØд¸¸Àa³ÉÔ±¡£
- ÊoÐÔºÍ*½*¨¾u¿É±»×ÓÀaÖØд¡£
- µ±¸¸ÀaµÄ*½*¨»oÊoÐÔÒѾ²»Âu×a×ÓÀaµÄÐeÇo£¬ÔoÐeÒªÖØд¡£
- Ò²¿ÉÄÜÒoΪÃuÃu²»¹ae*¶µ¼ÖÂÖØд¡£
˽ÓÐÊoÐÔ²»Äܱ»ÖØд£¬Ã¿¸o˽ÓÐÊoÐÔ¶¼»a±»¼Ç¼¡£ÔڼǼÊoÐÔÃuµÄͬʱ£¬»¹»a¼Ç¼Àa¡£
Èç¹uÓÐÄÚÖú¯Êý±»ÖØд£¬Ôo¿Éµ÷Óø¸Àa*½*¨¡£Èçµ÷Óø¸Àa¹¹Ôi*½*¨parent::__construct()
# ÖØдÏÞÖÆ
*ÃÎÊÏÞÖÆ£º
×ÓÀaµÄ³ÉÔ±µÄ*ÃÎÊ¿ØÖƱØÐeÏaµÈ»oÈoÓÚ¸¸Àa¡£
*½*¨²ÎÊýÏÞÖÆ£º
²ÎÊýÊýÁ¿±ØÐeÏaͬ£¬²ÎÊýÃu¿É²»Í¬¡£
# $thisÈ*¶¨ÔÔo
$thisΪµ÷ÓøÃ*½*¨µÄ¶ÔÏo£¬±iʾ¸Ã*½*¨µÄÖ´Ðл*¾³¶ÔÏo¡£
- ¶ÔÏoµ÷ÓÃ
- »*¾³µÄ´«µÝ¡£Èç¹uµ±Ç°µ÷ÓÃʱ£¬²»ÄÜÈ*¶¨$thisµÄÖµ(¾²Ì¬µ÷ÓÃ)£¬´Ëʱ¾²Ì¬µ÷ÓÃËu´¦¶ÔÏo»*¾³»a´«µÝµ½±»µ÷ÓõÄ*½*¨ÄÚ¡£
$this²¢*ÇÓÀÔ¶´u±i±¾¶ÔÏo£¬¶øÊÇÓÉ*½*¨µÄÖ´Ðл*¾³¾o¶¨¡£
# final
Èç¹u¸¸ÀaÖеÄ*½*¨±»ÉuÃ÷Ϊfinal£¬Ôo×ÓÀaÎÞ*¨¸²¸Ç£¨ÖØд£(C)¸Ã*½*¨¡£
Èç¹uÒ»¸oÀa±»ÉuÃ÷Ϊfinal£¬Ôo²»Äܱ»¼Ì³Ð¡£
µ«¼ÓÓÐfinal¹Ø¼u×ÖµÄÀaÒÀ¾ÉÄܱ»ÊµÀý»¯£¡
# ³eÏoÀa
¹Ø¼u×Ö£ºabstract
³eÏoÀa²»ÄÜÖ±½Ó±»ÊµÀý»¯£¬±ØÐeÏȼ̳иóeÏoÀa£¬È»ºoÔÙʵÀý»¯×ÓÀa¡£
³eÏoÀaÖÐÖÁÉÙÒª°uº¬Ò»¸o³eÏo*½*¨¡£*dzeÏoÀa²»ÄÜ°uº¬³eÏo*½*¨¡£
Èç¹uÀa*½*¨±»ÉuÃ÷Ϊ³eÏoµÄ£¬ÄÇôÆaÖоͲ»ÄÜ°uÀ¨¾ßÌaµÄ¹¦ÄÜʵÏÖ¡£³eÏo*½*¨²»ÄÜ°uº¬´oÀ¨ºÅ¼°*½*¨Ìa¡£
¼Ì³ÐÒ»¸o³eÏoÀaµÄʱºo£¬×ÓÀa±ØÐeʵÏÖ³eÏoÀaÖеÄËuÓгeÏo*½*¨¡£
¼´£¬×ÓÀa±ØÐeÖØд³eÏo¸¸ÀaÖеÄËuÓгeÏo*½*¨¡£
ÁiÍa£¬ÕaÐ(C)*½*¨µÄ¿É¼uÐÔ±ØÐeºÍ³eÏoÀaÖÐÒ»Ñu£¨»oÕ߸uΪ¿iËÉ£(C)¡£
¼´£¬Èç¹u³eÏoÀaÖÐij¸o³eÏo*½*¨±»ÉuÃ÷Ϊprotected£¬ÄÇô×ÓÀaÖÐʵÏÖµÄ*½*¨¾ÍÓ¦¸ÃÉuÃ÷Ϊprotected»oÕßpublic£¬¶ø²»Äܶ¨ÒaΪprivate¡£
- ³eÏoÀaµÄ×ÓÀaÖеÄÆÕͨ*½*¨Ö´ÐÐ*½Ê½ºÍÆaËuÀaÏaͬ¡£
- ×÷Óãº
1. ¼Ì³Ð£¬ÎªÀ(C)Õ¹Àa£¬Í³Ò»¹«¹²²Ù×÷¡£
2. ÏÞÖƽa¹¹£¨¹ae*¶£(C)¡£¹ae*¶×ÓÀaµÄ½a¹¹¡£
// ½Ó¿Ú
¹Ø¼u×Ö£ºinterface
- ¶ÔÏoÌa¹(C)µÄÓe¶ÔÏo½»»¥µÄ*½Ê½¾ÍÊǽӿڡ£
- ʹÓýӿڿÉÒÔÖ¸¶¨Ä³¸oÀa±ØÐeʵÏÖÄÄÐ(C)*½*¨£¬µ«²»ÐeÒª¶¨ÒaÕaÐ(C)*½*¨µÄ¾ßÌaÄÚÈÝ¡£
- ͨ¹ýinterfaceÀ´¶¨ÒaÒ»¸o½Ó¿Ú£¬¾ÍÏñ¶¨ÒaÒ»¸o±e×¼µÄÀaÒ»Ñu£¬µ«ÆaÖж¨ÒaËuÓеÄ*½*¨¶¼Êǿյġ£
- ½Ó¿ÚÖж¨ÒaµÄËuÓÐÊoÐÔºÍ*½*¨¶¼±ØÐeÊÇpublic£¬¿ÉÊ¡ÂÔpublic¹Ø¼u×Ö¡£
- ½Ó¿ÚÖÐÒ²¿ÉÒÔ¶¨Òa³£Á¿(const)¡£½Ó¿Ú³£Á¿ºÍÀa³£Á¿µÄʹÓÃÍeÈ«Ïaͬ¡£
¿ÉÒÔÓÃ::*ÃÎÊ¡£½Ó¿ÚÃu::³£Á¿Ãu£¬ÊµÏÖÀa::³£Á¿Ãu¡£
ËuÃǶ¼ÊǶ¨Öµ£¬¿ÉÒÔ±»×ÓÀa»o×Ó½Ó¿ÚʹÓ㬵«²»ÄÜÐ޸ġ£
- ½Ó¿Ú²»Äܶ¨ÒaÊoÐÔ£¡
# ¶¨Òa½Ó¿Ú
interface ½Ó¿ÚÃu {
½Ó¿ÚÄÚÈÝ£¨¹«¹²*½*¨ÉuÃ÷µÄ¼¯ºÏ£(C)
}
# ½Ó¿ÚʵÏÖ
- ҪʵÏÖÒ»¸o½Ó¿Ú£¬¿ÉÒÔʹÓÃimplements²Ù×÷*u¡£
- ÀaÖбØÐeʵÏÖ½Ó¿ÚÖж¨ÒaµÄËuÓÐ*½*¨£¬*ñÔo»a±¨Ò»¸ofatal´iÎo¡£
- Èç¹uҪʵÏÖ¶a¸o½Ó¿Ú£¬¿ÉÒÔÓöººÅÀ´*Ö¸o¶a¸o½Ó¿ÚµÄÃu³Æ¡£
- ʵÏÖ¶a¸o½Ó¿Úʱ£¬½Ó¿ÚÖеÄ*½*¨²»ÄÜÓÐÖØÃu¡£
- ½Ó¿ÚÒ²¿ÉÒԼ̳У¬Í¨¹ýʹÓÃextends²Ù×÷*u¡£
class ÀaÃu implements ½Ó¿ÚÃu {
½Ó¿Ú*½*¨µÄʵÏÖ
}
# ×¢Òa
1. ÀaÓe³eÏoÀaÖ®¼aÊǼ̳йØϵ£¬ÀaÓe½Ó¿ÚÖ®¼aÊÇʵÏÖ¹Øϵ¡£
2. ÀaÓe³eÏoÀaÊǵ¥¼Ì³Ð£¬ÀaÓe½Ó¿ÚÊǶaʵÏÖ¡£
3. ½Ó¿Ú²»ÊÇÀa£¬ÏÞÖÆÀaµÄ½a¹¹¡£
4. ½Ó¿ÚÓe½Ó¿ÚÖ®¼aÊǶa¼Ì³Ð¡£ÓÃextends¹Ø¼u×Ö¡£
interface I_C extends I_A, I_B {}
// ¾²Ì¬ÑÓ³Ù°o¶¨
self::£¬´u±i±¾Àa(µ±Ç°´uÂeËuÔÚÀa)
ÓÀÔ¶´u±i±¾Àa£¬ÒoΪÔÚÀa±aÒeʱÒѾ±»È*¶¨¡£
¼´£¬×ÓÀaµ÷Óø¸Àa*½*¨£¬selfÈ´²»´u±iµ÷ÓõÄ×ÓÀa¡£
static::£¬´u±i±¾Àa(µ÷ÓøÃ*½*¨µÄÀa)
ÓÃÓÚÔڼ̳Ð*¶Î§ÄÚÒýÓþ²Ì¬µ÷ÓõÄÀa¡£
ÔËÐÐʱ£¬²ÅÈ*¶¨´u±iµÄÀa¡£
static::²»ÔÙ±»½aÎoΪ¶¨Òaµ±Ç°*½*¨ËuÔÚµÄÀa£¬¶øÊÇÔÚʵ¼ÊÔËÐÐʱ¼ÆËaµÄ¡£
// ¶ÔÏoµÄ±eÀu£¨µu´u£(C)
- ¶ÔÏoͨ¹ýÊoÐÔ±£´aeÊý¾Ý£¬¹Ê±eÀu¶ÔÏoµÄÊoÐÔ¡£
- foreachÓiÑÔ½a¹¹£¬»ñµÃÊoÐÔÃuºÍÊoÐÔÖµ¡£
foreach ($obj as $p_name => $p_value) {}
# ×Ô¶¨Òa±eÀu(µu´uÆ÷Iterator)
Iterator - ¿ÉÔÚÄÚ²¿µu´u×Ô¼ºµÄÍa²¿µu´uÆ÷»oÀaµÄ½Ó¿Ú
Iterator::current ¡ª *µ»Øµ±Ç°ÔªËØ
Iterator::key ¡ª *µ»Øµ±Ç°ÔªËصļu
Iterator::next ¡ª ÏoÇ°Òƶ¯µ½ÏÂÒ»¸oÔªËØ
Iterator::rewind ¡ª *µ»Øµ½µu´uÆ÷µÄµÚÒ»¸oÔªËØ
Iterator::valid ¡ª ¼i²eµ±Ç°Î»ÖÃÊÇ*ñÓÐЧ
# ¶ÔÏoµÄ¿Ë¡
//¶ÔÏoÖ®¼aµÄ´«ÖµÊÇ[ÒýÓÃ]´«µÝ¡£
¿Ë¡£ºÐ¶ÔÏo = clone ¾É¶ÔÏo
- ËuÓеÄÒýÓÃÊoÐÔÈÔÈ»»aÊÇÒ»¸oÖ¸ÏoÔÀ´µÄ±aÁ¿µÄÒýÓá£
__clone()*½*¨ÔÚ¶ÔÏo±»¿Ë¡ʱ×Ô¶¯µ÷Óá£
×¢Òa£º¹¹Ôi*½*¨¶ÔӦʵÀý»¯(new)£¬¿Ë¡*½*¨¶ÔÓ¦¿Ë¡(clone)¡£
// µ¥Àýģʽ
#Èý˽һ¹«
µ¥Àýģʽ£¨Singleton£(C)ÓÃÓÚΪһ¸oÀaÉu³ÉÒ»¸oΨһµÄ¶ÔÏo¡£×i³£ÓõĵØ*½ÊÇÊý¾Ý¿aÁ¬½Ó¡£Ê¹Óõ¥ÀýģʽÉu³ÉÒ»¸o¶ÔÏoºo£¬¸Ã¶ÔÏo¿ÉÒÔ±»ÆaËuÖÚ¶a¶ÔÏoËuʹÓá£
# *ÀÖ¹Ò»¸oÀa±»ÊµÀý»¯¶a´Î
class MySQLDB {
private static $instance = null; // ´aeÀaʵÀýÔÚ´ËÊoÐÔÖÐ
// ¹¹Ôi*½*¨ÉuÃ÷Ϊprivate£¬*ÀÖ¹Ö±½Ó´´½¨¶ÔÏo
private function __construct() {}
public static function getInstance() {
if(! self::$instance instanceof static) {
self::$instance = new static;
}
return self::$instance;
}
private function __clone() {} // ×eÖ¹Óû§¸´ÖƶÔÏoʵÀý
}
// ħÊo*½*¨
__construct ¹¹Ôi*½*¨
__destruct Îo¹¹*½*¨
__clone ¿Ë¡¶ÔÏo
__sleep ÐoÁл¯¶ÔÏo
__wakeup *´ÐoÁл¯¶ÔÏo
__autoload ×Ô¶¯¼ÓÔØ£¬Ê¹ÓÃÀaµ«Î´ÕÒµ½Ê±
__toString ¶ÔÏo±»µ±×÷×Ö*u´®Ê¹ÓÃʱ
__invoke µ±³¢ÊÔÒÔµ÷Óú¯ÊýµÄ*½Ê½µ÷ÓÃÒ»¸o¶ÔÏoʱ
# ÖØÔØ overload
Ö¸¶¯Ì¬µØ"´´½¨"ÀaÊoÐÔºÍ*½*¨
Óû§¿ÉÒÔ×ÔÓɵÄΪ¶ÔÏoÌi¼Ó¶iÍaµÄÊoÐÔ£¬¸ÃÌØÐÔ¾ÍÊÇÖØÔØ¡£
ËuÓеÄÖØÔØ*½*¨¶¼±ØÐe±»ÉuÃ÷Ϊpublic¡£
µ±µ÷Óõ±Ç°»*¾³ÏÂ䶨Òa»o²»¿É¼uµÄÀaÊoÐÔ»o*½*¨Ê±£¬ÖØÔØ*½*¨»a±»µ÷Óá£
ÖØÔØÏa¹ØħÊo*½*¨µÄ²ÎÊý¶¼²»ÄÜͨ¹ýÒýÓô«µÝ¡£
# ÊoÐÔÖØÔØ
- ´¦Ài²»¿É*ÃÎʵÄÊoÐÔ
ÊoÐÔÖØÔØÖ»ÄÜÔÚ¶ÔÏoÖнøÐС£
# ÊoÐÔÖØÔضÔÓÚ¾²Ì¬ÊoÐÔÎÞЧ
ÔÚ¾²Ì¬*½*¨ÖУ¬ÕaÐ(C)ħÊo*½*¨½«²»»a±»µ÷Óá£ËuÒÔÕaÐ(C)*½*¨¶¼²»Äܱ»ÉuÃ÷Ϊstatic¡£
__set ÔÚ¸ø²»¿É*ÃÎʵÄÊoÐÔ¸³ÖµÊ±
public void __set(string $name, mixed $value)
×÷ÓãºÅuÁ¿¹ÜÀi˽ÓÐÊoÐÔ£¬¼a½Ó±£»¤¶ÔÏo½a¹¹
__get ¶ÁÈ¡²»¿É*ÃÎʵÄÊoÐÔµÄֵʱ
public mixed __get(string $name)
__isset µ±¶Ô²»¿É*ÃÎʵÄÊoÐÔµ÷ÓÃisset()»oempty()ʱ
public bool __isset(string $name)
__unset µ±¶Ô²»¿É*ÃÎʵÄÊoÐÔµ÷ÓÃunset()ʱ
public void __unset(string $name)
# *½*¨ÖØÔØ
- ´¦Ài²»¿É*ÃÎʵÄ*½*¨
__call µ±µ÷ÓÃÒ»¸o²»¿É*ÃÎʵÄ*Ǿ²Ì¬*½*¨£¨Èç䶨Òa£¬»oÕß²»¿É¼u£(C)ʱ×Ô¶¯±»µ÷ÓÃ
public mixed __call(string $name, array $arguments)
__callStatic µ±ÔÚµ÷ÓÃÒ»¸o²»¿É*ÃÎʵľ²Ì¬*½*¨£¨Èç䶨Òa£¬»oÕß²»¿É¼u£(C)ʱ×Ô¶¯±»µ÷ÓÃ
public static mixed __callStatic(string $name, array $arguments)
# $name²ÎÊýÊÇÒªµ÷ÓõÄ*½*¨Ãu³Æ¡£$arguments²ÎÊýÊÇÒ»¸oÊý×e£¬°uº¬×ÅÒª´«µÝ¸ø*½*¨µÄ²ÎÊý¡£
// ÀaÐÍÔ¼Êø
º¯ÊýµÄ²ÎÊý¿ÉÒÔÖ¸¶¨Ö»ÄÜΪ¶ÔÏo»oÊý×e
ÏÞ¶¨Îª¶ÔÏoÔoÔÚÐβÎÇ°¼ÓÀaÃu£¬ÏÞ¶¨ÎªÊý×eÔoÔÚÐβÎÇ°¼Óarray
ÀaÐÍÔ¼ÊøÔÊÐiNULLÖµ
ÀaÐÍÔ¼Êø²»Ö»ÊÇÓÃÔÚÀaµÄ³ÉÔ±*½*¨Ài£¬Ò²ÄÜʹÓÃÔÚº¯ÊýÀi¡£
// Èý´oÌØÐÔ
*a×°£ºÒþ²ØÄÚ²¿ÊÇÎuÄÉ£¬½o¿ª*¢½Ó¿Ú¡£
¼Ì³Ð£ºÒ»¸o¶ÔÏoµÄ³ÉÔ±±»ÁiÒ»¸o¶ÔÏoËuʹÓá£Ói*¨ÉÏÌaÏÖΪ´uÂeµÄ¹²Óá£
¶a̬£º¶aÖÖÐÎ̬¡£
// ÀaÓe¶ÔÏo¡¤¹Ø¼u×Ö
this ´u±i±¾¶ÔÏo
public ¹«Óеģ¨¼Ì³ÐÁ´¡¢±¾Àa¡¢Ía²¿¾u¿É*ÃÎÊ£(C)
protected ±£»¤µÄ£¨½o¼Ì³ÐÁ´¡¢±¾Àa¿É*ÃÎÊ£(C)
private ˽Óеģ¨½o±¾Àa¿É*ÃÎÊ£(C)
parent:: ´u±i¸¸Àa
self:: ´u±i±¾Àa(µ±Ç°´uÂeËuÔÚÀa)
static:: ´u±i±¾Àa(µ÷ÓøÃ*½*¨µÄÀa)
static ¾²Ì¬³ÉÔ±£¨ÊoÐÔ¡¢*½*¨£(C)£¬ËuÓжÔÏo¾u¿ÉʹÓã¬Ía²¿Ò²¿ÉÖ±½ÓʹÓûoÐ޸ģ¬¾²Ì¬*½*¨²»¿É*ÃÎÊ*Ǿ²Ì¬³ÉÔ±
final *½*¨ÓÃfinal²»¿É±»×ÓÀaÖØÔØ£¬ÀaÓÃfinal²»¿É±»¼Ì³Ð£¨*½*¨¡¢Àa£(C)
const Àa³£Á¿£¨ÊoÐÔ£(C)
abstract ³eÏoÀa
interface ½Ó¿Ú
extends Àa¼Ì³Ð(×ӽӿڼ̳нӿڡ¢ÆaËuÆÕͨÀa¼Ì³Ð)
implements ½Ó¿ÚʵÏÖ£¨ÀaʵÏÖ½Ó¿Ú¡¢³eÏoÀaʵÏÖ½e¿Ú£(C)£¨¶Ô½Ó¿ÚµÄʵÏֺͼ̳оu¿ÉÓжa¸o£(C)
Iterator ÄÚÖýӿڣ¨µu´u£(C)
clone ¿Ë¡
instance ʵÀý
instanceof ij¶ÔÏoÊÇ*ñÊoÓÚijÀa
/* ¡¾ÀaÓe¶ÔÏoÏa¹Øº¯Êý¡¿ */
class_alias([$original [,$alias]]) ¸øÀaÈ¡±ðÃu
class_exists($class [,$autoload]) ¼i²eÀaÊÇ*ñÒѶ¨Òa
interface_exists($interface [,$autoload]) ¼i²e½Ó¿ÚÊÇ*ñÒѱ»¶¨Òa
method_exists($obj, $method)¼i²eÀaµÄ*½*¨ÊÇ*ñ´aeÔÚ
property_exists($class, $property) ¼i²e¶ÔÏo»oÀaÊÇ*ñ¾ßÓиÃÊoÐÔ
get_declared_classes(void) *µ»ØÓÉÒѶ¨ÒaÀaµÄÃu×ÖËu×e³ÉµÄÊý×e
get_declared_interfaces(void) *µ»ØÒ»¸oÊý×e°uº¬ËuÓÐÒÑÉuÃ÷µÄ½Ó¿Ú
get_class([$obj]) *µ»Ø¶ÔÏoµÄÀaÃu
get_parent_class([$obj]) *µ»Ø¶ÔÏo»oÀaµÄ¸¸ÀaÃu
get_class_methods($class) *µ»ØÓÉÀaµÄ*½*¨Ãu×e³ÉµÄÊý×e
get_object_vars($obj) *µ»ØÓɶÔÏoÊoÐÔ×e³ÉµÄ¹ØÁªÊý×e
get_class_vars($class) *µ»ØÓÉÀaµÄĬÈÏÊoÐÔ×e³ÉµÄÊý×e
is_a($obj, $class) Èç¹u¶ÔÏoÊoÓÚ¸ÃÀa»o¸ÃÀaÊǴ˶ÔÏoµÄ¸¸ÀaÔo*µ»ØTRUE
is_subclass_of($obj, $class) Èç¹u´Ë¶ÔÏoÊǸÃÀaµÄ×ÓÀa£¬Ôo*µ»ØTRUE
get_object_vars($obj) *µ»ØÓɶÔÏoÊoÐÔ×e³ÉµÄ¹ØÁªÊý×e
// ³£ÓÃÀa
# PHPÊÖ²a -> Ô¤¶¨ÒaÀa
Closure ±Õ°uÀa£¬ÄaÃuº¯Êý¶ÔÏoµÄfinalÀa
stdClass ±e×¼Àa£¬Í¨³£ÓÃÓÚ¶ÔÏoÀa±£´ae¼¯ºÏÊý¾Ý
__PHP_Incomplete_Class ²»ÍeÕuÀa£¬µ±Ö»ÓжÔÏo¶øûÓÐÕÒµ½Àaʱ£¬Ôo¸Ã¶ÔÏo±»ÈÏΪÊǸÃÀaµÄ¶ÔÏo
Exception Òi³£Àa
PDO Êý¾Ý¶ÔÏoÀa
// ħÊo³£Á¿
__DIR__ ÎļþËuÔÚµÄĿ¼
__LINE__ ÎļþÖеĵ±Ç°ÐкÅ
__FILE__ ÎļþµÄÍeÕuÂ*¾¶£¨¾ø¶ÔÂ*¾¶£(C)ºÍÎļþÃu
__CLASS__ ÀaµÄÃu³Æ
__METHOD__ ÀaµÄ*½*¨Ãu£¬°uº¬ÀaÃuºÍ*½*¨Ãu
__FUNCTION__ º¯ÊýÃu³Æ£¬ÓÃÔÚ*½*¨ÄÚÖ»±iʾ*½*¨Ãu
// *´Éa»uÖÆ Reflection
×÷Óãº1. »ñÈ¡½a¹¹ÐÅÏ¢ 2. ´uÀiÖ´ÐÐ
ReflectionClass ±¨¸aeÒ»¸oÀaµÄÓйØÐÅÏ¢
ReflectionMethod ±¨¸aeÒ»¸o*½*¨µÄÓйØÐÅÏ¢
ReflectionClass::export Êa³oÀa½a¹¹±¨¸ae
# ´uÀiÖ´ÐÐ
ʵÀý»¯ ReflectionFunction ÀaµÄ¶ÔÏo
$f = new ReflectionFunction('func'); // funcΪº¯Êýfunc($p)
$f->invoke('param');
/* Ò³ÃaeÌøת */
// PHP
header('Loacation: url')
header()Ö´ÐÐÍe±Ïºo£¬ºoÃaeµÄ´uÂeÒ²»a¼ÌÐøÖ´ÐУ¬¹ÊÐeÔÚ¸ÃÓi¾aºo¼Ódie½aÊø
ÎÞ*¨¸ø³oÌaʾ£¬Ö±½ÓÌøת
// JS*½*¨
location.href = url
// HTML
<meta http-equiv="Refresh" content="±iʾʱ¼aµÄÊýÖµ; url=ÒªÌøתµÄURI">
/* ¡¾Cookie¡¿ */
cookieÊÇÒ»ÖÖÔÚÔ¶³Ìa¯ÀÀÆ÷¶Ë´¢´aeÊý¾Ý²¢ÒÔ´ËÀ´¸u×ÙºÍʶ±ðÓû§µÄ»uÖÆ¡£
cookieÊÇHTTP±eÍ*µÄÒ»²¿*Ö£¬Òo´Ësetcookie()º¯Êý±ØÐeÔÚÆaËuÐÅÏ¢±»Êa³oµ½a¯ÀÀÆ÷Ç°µ÷Óã¬ÕaºÍ¶Ôheader()º¯ÊýµÄÏÞÖÆÀaËÆ¡£¿ÉÒÔʹÓÃÊa³o»º³aº¯ÊýÀ´Ñӳٽű¾µÄÊa³o£¬Ö±µ½°´ÐeÒªÉeÖúÃÁËËuÓеÄcookie»oÕßÆaËuHTTP±eÍ*¡£
// ÐÂÔo
setcookie ÐÂÔoÒ»ÌocookieÐÅÏ¢
setcookie($name [,$value [,$expire [,$path [,$domain [,$secure [,$httponly]]]]]])
#×¢Òa£ºsetcookie()º¯ÊýÇ°²»ÄÜÓÐÊa³o£¡³ý*Ç¿ªÆoob»º´ae£¡
# ²ÎÊý˵Ã÷
$name - cookieµÄʶ±ðÃu³Æ