À°§A¾ÛÀòµøıµJÂI¡A±M·~¼s§i©ÛµP³]­p»s§@
Åý¤½¥qªù­±§ó¥[«G²´¡A¬°§A³Ð³y°Ó¾÷¡I
¨t²Î®a¨ãºû­×¦U¼tµP§N®ð¡Gµ¡«¬¡B¤ÀÂ÷¦¡¡B¤ô§N¦¡½c«¬µ¥«O¾i
§N´C¡Bº|¤ô¡BÀ£ÁY¾÷ºû­×«O©T¡C

­º­¶  ¡E  j2h ½×¾Â ¡E µ{¦¡³]­p°Q½×     ¡E 

[±Ð¾Ç]PHP ¤¤ªºÃþclass

©ÐªF¡G¬P·Ý
µoªí®É¶¡¡G2007-01-19



PHP ¤¤ªºÃþ(class)¡Ð¤°»ò¥sÃþ(class)?


¦bÄÄ­zÃþªº·§©À¤§«e§Ú­Ì¨Ó¥ý»¡»¡­±¦Vª«¥ó½sµ{ªº·§©À¡G­±¦Vª«¥óªºµ{¦¡³]­p(Object-Oriented Programming¡A²°O¬°OOP)¥ß·N©ó³Ð«Ø³nÅé­«¥Î¥N½X¡A¨ã³Æ§ó¦n¦aÃþ¤ñ²{¹ê¥@¬ÉÀô¹Òªº¯à¤O¡A³o¨Ï¥¦³Q¤½»{¬°¬O¦Û¤W¦Ó¤U½sµ{ªºÀu³ÓªÌ¡C¥¦³q¹Lµ¹µ{¦¡¤¤¥[¤JÂX®i»y¥y¡A§â¨ç¼Æ"«Ê¸Ë"¶i½sµ{©Ò¥²»Ýªº"ª«¥ó"¤¤¡C­±¦Vª«¥óªº½sµ{»y¨¥¨Ï±o½ÆÂøªº¤u§@±ø²z²M´·¡B½s¼g®e©ö¡C»¡¥¦¬O¤@³õ­²©R¡A¤£¬O¹ïª«¥ó¥»¨­¦Ó¨¥¡A¦Ó¬O¹ï¥¦­Ì³B²z¤u§@ªº¯à¤O¦Ó¨¥¡Cª«¥ó¨Ã¤£»P¶Ç²Îµ{¦¡³]­p©M½sµ{¤èªk¬Û®e¡A¥u¬O³¡¤À­±¦Vª«¥ó¤Ï¦Ó·|¨Ï±¡§Î§óÁV¡C°£«D¾ã­Ó¶}µoÀô¹Ò³£¬O­±¦Vª«¥óªº¡A§_«hª«¥ó²£¥Íªº¦n³BÁÙ¨S¦³±a¨Óªº³Â·Ð¦h¡C¦³¤H¥i¯à·|»¡PHP¤£¬O¤@­Ó¯u¥¿ªº­±¦Vª«¥ó½sµ{ªº»y¨¥¡A PHP ¬O¤@­Ó²V¦X«¬ »y¨¥¡A§A¥i¥H¨Ï¥Î­±¦Vª«¥ó½sµ{¡A¤]¥i¥H¨Ï¥Î¶Ç²Îªº¹Lµ{¤Æ½sµ{¡CµM¦Ó¡A¹ï©ó¤j«¬±M®×ªº¶}µo¡A§A¥i¯à·Q»Ý­n¦bPHP¤¤¨Ï¥Î¯Âªº­±¦Vª«¥ó½sµ{¥hÁn©úÃþ¡A¦Ó¥B¦b§Aªº±M®×¶}µo¥u¥Îª«¥ó©MÃþ¡CÀHµÛ±M®×¶V¨Ó¶V¤j¡A¨Ï¥Î­±¦Vª«¥ó½sµ{¥i¯à·|¦³À°§U¡A­±¦Vª«¥ó½sµ{¥N½X«Ü®e©öºûÅ@¡A®e©ö²z¸Ñ©M­«´_¨Ï¥Î¡A³o¨Ç´N¬O³nÅé¤uµ{ªº°ò¦¡C¦b°ò©óWebªº±M®×¤¤À³¥Î³o¨Ç·§©À´N¦¨¬°±N¨Óºô¯¸¦¨¥ªº¦]¯À¡C

¡@¡@ª«¥ó¡]Object¡^¬O°ÝÃD°ì©Î¹ê²{°ì¤¤¬Y¨Ç¨Æª«ªº¤@­Ó©â¶H¡A¥¦¤Ï¬M¦¹¨Æª«¦b¨t²Î¤¤»Ý­n«O¦sªº¸ê°T©Mµo´§ªº§@¥Î¡F¥¦¬O¤@²ÕÄÝ©Ê©M¦³Åv¹ï³o¨ÇÄݩʶi¦æ¾Þ§@ªº¤@²ÕªA°Èªº«Ê¸ËÅé¡C Ãö©óª«¥ó­n±q¨â¤è­±²z¸Ñ¡G¤@¤è­±«ü¨t²Î©Ò­n³B²zªº²{¹ê¥@¬É¤¤ªºª«¥ó¡F¥t¤@¤è­±ª«¥ó¬O¹q¸£¤£ª½±µ³B²zªºª«¥ó¡A¦Ó¬O³B²z¬ÛÀ³ªº¹q¸£ªí¥Ü¡A³oºØ¹q¸£ªí¥Ü¤]ºÙ¬°ª«¥ó¡C²³æªº¨Ó»¡¡A¤@­Ó¤H´N¬O¤@­Óª«¥ó¡A¤@­Ó¤Ø¤l¤]¥i¥H»¡¬O­Óª«¥ó¡C·í³o¨Çª«¥ó¥i¥H¥Î¸ê®Æª½±µªí¥Ü®É¡A§Ú­Ì´NºÙ¥L¬°ÄÝ©Ê¡A¤Ø¤lªº«×¶q³æ¦ì¥i¥H¬OÂç¦Ì¡A¤½¤Ø©Î­^¤Ø¡A³o­Ó«×¶q³æ¦ì´N¬O¤Ø¤lªºÄÝ©Ê¡C
¡@¡@
¡@¡@¦bPHPùاڭ̥i¥H©w¸q¤@­ÓÃþ¡AÃþ¡]Class¡^´N¬O«üÅܼƻP¤@¨Ç¨Ï¥Î³o¨ÇÅܼƪº¨ç¼Æªº¶°¦X¡CPHP¬O¤@ºØÃP´²Ãþ«¬ªº»y¨¥¡A©Ò¥H³q¹LÃþ«¬­«¸ü¤£°_§@¥Î¡A³q¹L°Ñ¼Æªº­Ó¼Æ¤£¦P¨Ó­«¸ü¤]¤£°_§@¥Î¡C ¦³®É¦b­±¦V¤¤­«¸üºc³y¨ç¼Æ«D±`¦n¡A³o¼Ë§A¥i¥H³q¹L¤£¦Pªº¤èªk³Ð«Øª«¥ó¡]¶Ç»¼¤£¦P¼Æ¶qªº°Ñ¼Æ¡^¡C¦bPHP¤¤´N¬O³q¹LÃþ¨Ó¹ê²{ªº¡C

¦bPHP¤¤¬O³q¹LÃþ¨Ó§¹¦¨¸ê°T«Ê¸Ëªº¡A¦bPHP¤¤©w¸qÃþªº»yªk¬O¡G


CODE:
class Class_name // ¦b­±¦Vª«¥ó½sµ{Ãþ¤¤¡A²ßºD¤WÃþªº²Ä¤@­Ó¦r¤¸¬°¤j¼g¡A¨Ã¥B¥²¶·²Å¦XÅܼƪº©R¦W³W«h¡C
{

//¨ç¼Æ»PÅܼƪº¶°¦X

}
?>

¡@¡@¦b©w¸qÃþ®É§A¥i¥H«ö¦Û¤wªº³ß¦nªº®æ¦¡¶i¦æ©w¸q¡A¦ý³Ì¦n¯à«O«ù¤@ºØ¼Ð·Ç¡A³o¼Ë¶}µo°_¨Ó·|§ó¦³®Ä¨Ç¡C

¡@¡@¸ê®Æ¦¨­û¦bÃþ¤¤¨Ï¥Î"var"Án©ú¨Ó©w¸q¡A¦bµ¹¸ê®Æ¦¨­û½á­È¤§«e¡A¥¦­Ì¬O¨S¦³Ãþ«¬ªº¡C¤@­Ó¸ê®Æ¦¨­û¥i¥H¬O¤@­Ó¾ã¼Æ¡A¤@­Ó°}¦C¡A¤@­Ó¬ÛÃö°}¦C(Associative Array)©ÎªÌ¬O¤@­Óª«¥ó¡C

¤U­±¬O¤@­ÓÃþ©w¸qªº¹ê»Ú¨Ò¤l¡G


CODE:
class Student
{
var $str_Name; //©m¦W
var $str_Sex; //©Ê§O
var $int_Id; //¾Ç¸¹
var $int_English; //­^»y¦¨ÁZ
var $int_maths; //¼Æ¾Ç¦¨ÁZ
}
?>

¡@¡@³o¬O¤@­Ó«Ü´¶³q©w¸qÃþªºÂ²³æ¨Ò¤l¡A¥Î©óÅã¥Ü¾Ç¥Íªº¾Ç²ß¦¨ÁZ¡AÃþ¦W¬°Student¡AStudentÃþ¥]²[¤F¤@­Ó¾Ç¥Íªº°ò¥»ÄÝ©Ê¡G©m¦W¡B©Ê§O¡B¾Ç¸¹¡B­^»y¦¨ÁZ©M¼Æ¾Ç¦¨ÁZ¡C
¡@¡@
¡@¡@function§Ú­ÌºÙ¤§¬°¦bÃþ¤¤³Q©w¸qªº¨ç¼Æ¡A¦b¨ç¼Æ¤¤³X°ÝÃþ¦¨­ûÅܼƮɡA§AÀ³¸Ó¨Ï¥Î$this->var_name¡A¨ä¤¤var_name«üªº¬OÃþ¤¤³QÁn©úªºÅܼơA§_«h¹ï¤@­Ó¨ç¼Æ¨Ó»¡¡A¥¦¥u¯à¬O§½³¡ÅܼơC §Ú­Ì¥ý©w¸q¤@­ÓInput()ªº¨ç¼Æ¡A¥Î¨Óµ¹¹ê¨Ò¤¤ªºª«¥ó½á¥Hªì­È¡G


CODE:
function Input ( $Name, $Sex, $Id, $Englis, $Maths)
{
$this->str_Name=$Name;
$this->str_Sex =$Sex;
$this->int_Id =$Id;
$this->int_Englis=$English;
$this->int_Maths=$Maths;
}

²{¦b§Ú­Ì¦A©w¸q¤@­Ó¥s"ShowInfo()"ªº¨ç¼Æ¡A¥Î©ó¦C¦L¾Ç¥Íªº°ò¥»±¡ªp¡G


CODE:
function ShowInfo() //©w¸qShowInfo()¨ç¼Æ
{
echo ("©m¦W¡G$this->str_Name

");
echo ("©Ê§O¡G$this->str_Sex

");
echo ("¾Ç¸¹¡G$this->int_Id

");
echo ("­^»y¦¨ÁZ¡G$this->int_English

");
echo ("¼Æ¾Ç¦¨ÁZ¡G$this->int_Maths

");
}

¦Ó©w¸q¦nªºÃþ«h¥²¶·¨Ï¥ÎnewÃöÁä¦r¨Ó¥Í¦¨ª«¥ó¡G


CODE:
$A_student=new Student;

¨Ò¦p§Ú­Ì­n¬°¤@­Ó¦W¬°$Wingªºª«¥ó³Ð«Ø¹ê¨Ò¡A¨Ã¶i¦æ½á­È¡A¥i¥H¨Ï¥Î¤U­±ªº¥N½X¡G


CODE:
$Wing =new Student; //¥ÎnewÃöÁä¦r¨Ó¥Í¦¨ª«¥ó
$Wing ->Input ("Wing","¨k",33,95,87);
//¤À§O¿é¤JWingªº©m¦W¡B©Ê§O¡B¾Ç¸¹¡B­^»y¦¨ÁZ¡B¼Æ¾Ç¦¨ÁZ¡A¨ä¤¤©m¦W©M©Ê§O¬O¦r¤¸«¬ÅܼơA©Ò¥H»Ý­n¥ÎÂù¤Þ¸¹¡A¨ä¥L¬°¼Æ­È«¬Åܼƫh¤£»Ý­n¡C

³q¹L¤U­±³o¬q§¹¾ãªº·½¥N½X¡A§Ú­Ì´N¥i¥H«Ü²M·¡ªº¬Ý¨ìÃþ¦bPHP¬O«ç»ò³Q¹B¥Îªº¡G


CODE:
class Student
{
var $str_Name;
var $str_Sex;
var $int_Id;
var $int_English;
var $int_maths;

function Input ( $Name, $Sex, $Id, $English, $Maths)
{
$this->str_Name=$Name;
$this->str_Sex =$Sex;
$this->int_Id =$Id;
$this->int_English=$English;
$this->int_Maths=$Maths;
}
function ShowInfo()
{
echo ("©m¦W¡G$this->str_Name

");
echo ("©Ê§O¡G$this->str_Sex

");
echo ("¾Ç¸¹¡G$this->int_Id

");
echo ("­^»y¦¨ÁZ¡G$this->int_English

");
echo ("¼Æ¾Ç¦¨ÁZ¡G$this->int_Maths

");
}
}


$Wing = new Student;
$Wing->Input ("Wing","¨k",33,95,87);
$Paladin = new Student;
$Paladin->Input ("paladin","¤k",38,58,59.5);

$Wing->ShowInfo();
$Paladin->ShowInfo();

?>

°õ¦æµ²ªGÀ³¬O³o¼Ëªº:
©m¦W¡GWing
©Ê§O¡G¨k
¾Ç¸¹¡G33
­^»y¦¨ÁZ¡G95
¼Æ¾Ç¦¨ÁZ¡G87
©m¦W¡GPaladin
©Ê§O¡G¤k
¾Ç¸¹¡G38
­^»y¦¨ÁZ¡G58
¼Æ¾Ç¦¨ÁZ¡G59.5

¡@¡@PHP²{¦³ªºª©¥»¸û¥H«eªºª©¥»¦b¹ï­±¦Vª«¥ó½sµ{ªº¤ä´©¤è­±¦³¤F«Ü¤jªº§ïµ½¡A¦ý¤ä´©ªºÁÙ¤£¬O«Ü§¹¾ã¡A¤£¹L²{¶¥¬qPHP¹ï­±¦Vª«¥ó½sµ{»y¨¥´£¨Ñªº¤ä´©¤£¦ý¦³§Q©ó§Ú­Ì³]­pµ{¦¡ªºµ²ºc¡A¹ï©ó¹ïµ{¦¡ªººûÅ@¤]¯à´£¨Ñ«Ü¤jªºÀ°§U¡C
;)



  • ÃÙ§Uºô¯¸       

    ¼s§Q¤£°Ê²£-ªO¾ô¦b¦a¥Í®Ú³Ì¹ê¦b--·sªO¯S°Ï«ü¦W«×³Ì°ª¡B­È±o±z«H¿àªº¦n©Ð¥ò
    §¹¾ã©Ð°T¡A©Ð«Î¡B©±­±¼öªùºë¿ïª«¥ó¡A¼s§Q¤£°Ê²£ Àu½è¥ò¤¶¡A©Ð«Î¯²¸î¡B¶R½æ¸ê°T³z©ú¡A¥æ©ö¯u¦w¤ß¡I
    ¼s§Q¤£°Ê²£-·sªO¯S°Ï«ü¦W«×³Ì°ª¡B­È±o±z«H¿àªº¦n©Ð¥ò
    ±zªº¦«¥I,¼s§Q¥Î¤ß¬°±zªA°È



  • ¡@¦@ 0 ¤H¦^À³

    ©m¦W¡G
    §G§i¤º®e¡G
    ¨ä¥L¿ï¶µ: