PHPÀàʵÀý½Ì³Ì£¨Ê®Áù£©£ºPHP5Öеij£Á¿
ÔÚPHP5ÀàÖмÌÐøʹÓÃconsÐÞÊγ£Á¿¡£ÎÒÃÇʹÓÃconst¶¨ÒåÒ»¸ö³£Á¿£¬¶¨ÒåµÄÕâ¸ö³£Á¿²»Äܱ»¸Ä±ä¡£ÔÚPHP5ÖÐconst¶¨ÒåµÄ³£Á¿Ó붨Òå±äÁ¿µÄ·½·¨²»Í¬£¬²»ÐèÒª¼Ó$ÐÞÊηû¡£ const PI = 3.14£» ÕâÑù¾Í¿ÉÒÔ¡£
¶øʹÓÃconst ¶¨ÒåµÄ³£Á¿Ãû³ÆÒ»°ã¶¼´óд£¬ÕâÊÇÒ»¸öÔ¼¶¨£¬ÔÚÈκÎÓïÑÔÖж¼ÊÇÕâÑù¡£
Èç¹û¶¨ÒåµÄ³£Á¿Óɶà¸öµ¥´Ê×é³É£¬Ê¹Óà _ Á¬½Ó£¬ÕâÒ²ÊÇÔ¼¶¨¡£
±ÈÈ磬 MAX_MUMBER ÕâÑùµÄÃüÃû·½Ê½¡£Ò»¸öÁ¼ºÃµÄÃüÃû·½Ê½£¬ÊdzÌÐòÔ±±ØÐë×¢ÒâµÄ¡£
ÀàÖеij£Á¿Ê¹ÓÃÆðÀ´ÀàËƾ²Ì¬±äÁ¿£¬²»Í¬µãÖ»ÊÇËüµÄÖµ²»Äܱ»¸Ä±ä¡£ÎÒÃÇʹÓÃÀàÃû::³£Á¿ÃûÀ´µ÷ÓÃÕâ¸ö³£Á¿¡£
<?php
//ÉùÃ÷Ò»¸öfinalÀàMath
class Math
{
const PI = 3.14;
public function __construct() {
return "ÕâÊÇMathÀà";
}
//ÕâÀïдÁËÒ»¸öËãÔ²Ãæ»ýµÄ·½·¨£¬Ê¹ÓÃÁËConst³£Á¿£¬
//×¢ÒâʹÓõķ½·¨£¬ÀàËÆÓÚ¾²Ì¬±äÁ¿¡£
public final function area($r) {
return $r * $r * self::PI;
}
public final function max($a, $b) {
return $a > $b ? $a : $b;
}
}
echo Math::PI;
?>
³¢ÊÔΪconst¶¨ÒåµÄ³£Á¿¸³Öµ£¬½«»á³öÏÖ´íÎó¡£
<?php
//˵Ã÷Ò»¸öfinalÀàMath
class Math
{
const PI = 3.14;
public function __toString() {
return "ÕâÊÇÒ»¸öMathÀà";
}
//ÕâÀïдÁËÒ»¸öËãÔ²Ãæ»ýµÄ·½·¨£¬Ê¹ÓÃÁËConst³£Á¿£¬
//×¢ÒâʹÓõķ½·¨£¬ÀàËÆÓ뾲̬±äÁ¿¡£
public final function area($r) {
return $r * $r * self::PI;
}
public final function max($a, $b) {
return $a > $b ? $a : $b;
}
public function setPI($a) {
self::PI = 3.1415;
}
}
echo Math::PI;
?>
Ïà¹ØÎĵµ£º
<html>
<head>
<title>¼ÆËã±¾ÖÜ¿ªÊ¼½áÊøÈÕÆÚ</title>
</head>
<body>
<form method="post" action="./index.html" enctype="utf-8">
<table>
<tr><td>ÊäÈëÄê·Ý</td><td><input name="year" type="text" maxlength="4" /></td> ......
<?php
/*
Singleton && Registry Design Pattern Implementation
*/
class Registry {
private $_store = array();
public function getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = new ......
PHP£¬Ò»¸öǶÌ×µÄËõдÃû³Æ£¬ÊÇÓ¢Îij¬¼¶Îı¾Ô¤´¦ÀíÓïÑÔ£¨PHP:Hypertext Preprocessor£©µÄËõд¡£PHP ÊÇÒ»ÖÖ HTML ÄÚǶʽµÄÓïÑÔ£¬PHPÓë΢ÈíµÄASPÆÄÓм¸·ÖÏàËÆ£¬¶¼ÊÇÒ»ÖÖÔÚ·þÎñÆ÷¶ËÖ´ÐеÄǶÈëHTMLÎĵµµÄ½Å±¾ÓïÑÔ£¬ÓïÑԵķç¸ñÓÐÀàËÆÓÚCÓïÑÔ£¬ÏÖÔÚ±»ºÜ¶àµÄÍøÕ¾±à³ÌÈËÔ±¹ã·ºµÄÔËÓá£PHP ¶ÀÌصÄÓï·¨»ìºÏÁË C¡¢Java¡¢Perl ÒÔ¼° PHP ×Ô ......
¹¹Ôì·½·¨ÓÖ³ÆΪ¹¹Ô캯Êý£¬ÊǶÔÏó±»´´½¨Ê±×Ô¶¯µ÷Óõķ½·¨£¬ÓÃÀ´Íê³ÉÀà³õʼ»¯µÄ¹¤×÷¡£
¹¹Ô캯ÊýºÍÆäËüº¯ÊýÒ»Ñù£¬¿ÉÒÔ´«µÝ²ÎÊý£¬¿ÉÒÔÉ趨²ÎÊýĬÈÏÖµ¡£¹¹Ô캯Êý¿ÉÒÔµ÷ÓÃÊôÐÔ£¬¿ÉÒÔµ÷Ó÷½·¨¡£¹¹Ô캯Êý¿ÉÒÔ±»ÆäËü·½·¨ÏÔʽµ÷Óá£
ÔÚPHP4 ÖÐʹÓÃÓëÀàÃûͬÃûµÄ·½·¨Îª¹¹Ô캯Êý¡£ÔÚPHP5ÖÐÒÀȻ֧³ÖÁËÕâÖÖ·½Ê½£¬µ«²»½¨ÒéÔÙʹÓÃÕâÖÖ·½Ê½¡ ......
PHP5ÖÐΪ½â¾ö±äÁ¿µÄÃüÃû³åÍ»ºÍ²»È·¶¨ÐÔÎÊÌ⣬ÒýÈë¹Ø¼ü×Ö“$this”´ú±íÆäËùÔÚµ±Ç°¶ÔÏó¡£
$thisÔÚ¹¹Ô캯ÊýÖÐÖ¸¸Ã¹¹Ô캯ÊýËù´´½¨µÄжÔÏó¡£
ÔÚÀàÖÐʹÓõ±Ç°¶ÔÏóµÄÊôÐԺͷ½·¨£¬±ØÐëʹÓÃ$this->È¡Öµ¡£
·½·¨Äڵľֲ¿±äÁ¿£¬²»ÊôÓÚ¶ÔÏ󣬲»Ê¹ÓÃ$this¹Ø¼ü×ÖÈ¡Öµ¡£
¾Ö²¿±äÁ¿ºÍÈ«¾Ö±äÁ¿Óë $this ¹Ø¼ü×Ö,ʹÓõ±Ç°¶ÔÏóµ ......