PHP5ÃæÏò¶ÔÏó£ºObject Cloning
PHP4:
<?
$sample1 = new StdClass();
$sample1->name = "Hasin";
$sample2 = $sample1;
$sample2->name = "Afif";
echo $sample1->name;
?>
In PHP4 it works differently; it will output Hasin, as both are different from
each other.
PHP5:
<?
$sample1 = new StdClass();
$sample1->name = "Hasin";
$sample2 = $sample1;
$sample2->name = "Afif";
echo $sample1->name;
?>
The output is Afif
PHP5:
<?
$sample1 = new stdClass();
$sample1->name = "Hasin";
$sample2 =clone $sample1;
$sample2->name = "Afif";
echo $sample1->name;
?>
The output now would be Hasin.
__clone·½·¨µÄʹÓÃ
ĬÈϵØ,ÓÃ__clone·½·¨½«½¨Á¢Ò»¸öÓëÔ¶ÔÏóÓµÓÐÏàͬÊôÐԺͷ½·¨µÄ¶ÔÏó. Èç¹ûÄãÏëÔÚ¿Ë¡ʱ¸Ä±äĬÈϵÄÄÚÈÝ,ÄãÒªÔÚ__cloneÖи²Ð´(ÊôÐÔ»ò·½·¨).
¿Ë¡µÄ·½·¨¿ÉÒÔûÓвÎÊý,µ«Ëüͬʱ°üº¬thisºÍthatÖ¸Õë(thatÖ¸Ïò±»¸´ÖƵĶÔÏó). Èç¹ûÄãÑ¡Ôñ¿Ë¡×Ô¼º,ÄãҪСÐĸ´ÖÆÈκÎÄãÒªÄãµÄ¶ÔÏó°üº¬µÄÐÅÏ¢,´Óthatµ½this. Èç¹ûÄãÓÃ__cloneÀ´¸´ÖÆ. PHP²»»áÖ´ÐÐÈκÎÒþÐԵĸ´ÖÆ,
ÏÂÃæÏÔʾÁËÒ»¸öÓÃϵÁÐÐòÊýÀ´×Ô¶¯»¯¶ÔÏóµÄÀý×Ó:
class ObjectTracker //¶ÔÏó¸ú×ÙÆ÷
{
private static $nextSerial = 0;
private $id;
private $name;
function __construct($name) //¹¹Ô캯Êý
{
$this->name = $name;
$this->id = ++self::$nextSerial;
}
function __clone() //¿Ë¡
{
$this->name = "Clone of $this->name";
$this->id = ++self::$nextSerial;
}
function getId() //»ñÈ¡idÊôÐÔµÄÖµ
{
return($this->id);
}
function getName() //»ñÈ¡nameÊôÐÔµÄÖµ
{
return($this->name);
}
}
$ot = new ObjectTracker("Zeev's Object");
$ot2 = clone$ot;
//Êä³ö: 1 Zeev's Object
print($ot->getId() . " " . $ot->getName() . "
");
//Êä³ö: 2 Clone of Zeev's Object
print($ot2->getId() . " " . $ot2->getName() . "
");
?>
Ïà¹ØÎĵµ£º
ÏÖÔÚ»¹ÓкܶàÈ˸㲻¶®PHPºÍJAVA¿ª·¢µÄÐͬOAµ½µ×ÓÐʲô²»Í¬£¬¶Ô×Ô¼ºµ½µ×ÓÐʲôӰÏ죬ÒÔÖÁÓÚ×ö³öÁË´íÎóµÄÑ¡Ôñ¶øêݺ¦ÎÞÇî¡£±ÊÕ߸ù¾ÝÁ½ÖÖ¼¼ÊõµÄÌصãÖØÐÂÕûÀíÁËÒ»¸ö¶Ô±È£¬¸ø¹ØÐÄÐͬOA¼¼ÊõµÄ¶ÁÕß×ö¸ö²Î¿¼¡£
»ªÌ춯Á¦ÐͬOAÊDzÉÓÃJAVA¼¼ÊõµÄµäÐÍ´ú±í£¬²¢¾ßÓÐһϵÁÐÓÅ»¯É ......
PHPÕýÔò±í´ïʽÖ÷ÒªÓÃÓÚ×Ö·û´®µÄģʽ·Ö¸î¡¢Æ¥Åä¡¢²éÕÒ¼°Ìæ»»²Ù×÷¡£Ê¹ÓÃÕýÔò±í´ïʽÔÚijЩ¼òµ¥µÄ»·¾³Ï¿ÉÄÜЧÂʲ»¸ß£¬Òò´ËÈçºÎ¸üºÃµÄʹÓÃPHPÕýÔò±í´ïʽÐèÒª×ۺϿ¼ÂÇ¡£
ÎÒµÄPHPÕýÔòÈëÃÅ£¬ÊÇÆðÔ´ÓÚÍøÉϵÄһƪÎÄÕ£¬ÕâƪÎÄÕÂÓÉdzÈëÉîµÄ²ûÊöÁËPHPÕýÔò±í´ïʽʹÓõķ½·¨£¬ÎÒ¾õµÃÊÇÒ»¸öºÜºÃµÄÈëÃŲÄÁÏ£¬²»¹ýѧ³É»¹ÊÇÒª¿¿¸öÈË£¬ÔÚʹÓà ......
¹úÇ쳤¼Ù½«ÖÁ£¬ÕæÊÇ¿ªÐÄ¡£ÕâÁ½ÌìÓеãС¿Õ£¬·ÒëÁËÊÖ²áÖеÄһЩÄÚÈÝ£¬ËãÊǸøPHPerÃǵÄÒ»¸öСÀñÎï¡£
±¾À´Ïë°ÑËùÓÐPHP5.3µÄÐÂÄÚÈݸø¸üÐÂÁË£¬µ«·¢ÏÖÊÖ²áÓкܴóµÄ¸Ä¶¯£¬ÓÈÆäÊÇһЩĿ¼½á¹¹Éϵķ¢¶¯¡£±ØÐëÏȸúÉÏÕâЩ¸Ä¶¯£¬·ñÔò±àÒë»á³ö´í£¬¸ü̸²»ÉϽøÒ»²½µÄ·Òë¡£
ÕâÁ½ÌìÖ÷Òª×öÁËÒÔϹ¤×÷£º
1. ½«·Òëƽ̨ǨÒƵ½SVN
PHP¹Ù·½µÄ ......
×÷Õß ³ÂºÆ £¨Haohappy£©
MSN: haohappy # php.net
2009-08-13
±¾ÎÄ¿¯ÓÚ¡¶³ÌÐòÔ±¡·ÔÓÖ¾ 2009.09
תÔØÇë×¢Ã÷×÷Õß¼°³ö´¦
ºÁÎÞÒÉÎÊ£¬Èç½ñPHPÒѾ³ÉΪWEB¿ª·½µ±ÖÐ×îÈÈÃŵļ¼ÊõÖ®Ò»¡£¸ù¾Ýnexen.netµÄµ÷²é£¬»¥ÁªÍøÉÏÈý·ÖÖ®Ò»µÄÍøվѡÔñPHPÀ´¿ª·¢·þÎñÆ÷¶Ë³ÌÐò¡£ÔÚÅ·ÃÀºÍÈÕ±¾µÈ¹ú¼Ò£¬PHP¿ª·¢Êг¡³ÊÏÖ³öһƬÐÀÐ ......
ËùÐèÈí¼þ£¨×¢Òâ°æ±¾£¡£©£º
Apache2.2.3
PHP5.1.5
MySQL5.0.24
ÕâÈý¸öÈí¼þ¶¼ÊÇÃâ·ÑµÄ£¬¿É´Ó¹ÙÍøÉÏÏÂÔØ£¬Ä¿Ç°ÎÒËùÓеÄÈí¼þÃûΪ£º
apache_2.2.3-win32-x86-no_ssl.msi
mysql-5.0.24-win32.zip
php-5.1.5-Win32.zip
»ùÓÚwindows²Ù×÷ϵͳ£¬ÔÚWindows XPÏ°²×°Ê¹Óãº
1¡¢°²×°¹ý³Ì£º
Ê×ÏÈ°²×°Apache·þÎñÆ÷£¬Ë«»÷apa ......