PHP編程與應用
(二)、REQUIRE語句
REQUIRE語句用指定的文件代替自己,很象 C 中的預處理 #include 。
這意味著你不能為了每次調用該函數來包含不同文件的內容,而把require()語句放在一個循環結構,。要這么做,使用 INCLUDE 語句。
require('header.inc');
(三)、 INCLUDE語句
INCLUDE語句包含指定的文件。
每次遇到INCLUDE是INCLUDE語句就包含指定的文件。所以你可以在一個循環結構中使用INCLUDE語句以包含一系列不同的文件。
$files = array('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
include($files[$i]);
}
(四)、 函數
可以通過以下的語法定義函數:
function foo( $arg_1, $arg_2, ..., $arg_n ) {
echo "Example function.\n";
return $retval;
}
函數中可以使用任何有效的PHP3 代碼,甚至是其他的函數或類 的定義
1、 函數返回值
函數可以通過可選的return語句返回值。返回值可以是任何類型,包括列表和對象。
function my_sqrt( $num ) {
return $num * $num;
}
echo my_sqrt( 4 ); // outputs '16'.
函數不能同時返回多個值,但可以通過返回列表的方法來實現:
function foo() {
return array( 0, 1, 2 );
}
list( $zero, $one, $two ) = foo();
2、 參數
外部信息可以通過參數表來傳入函數中;參數表就是一系列逗號分隔的變量和/或常量。
PHP3支持通過值形參數(默認), 變量參數,和 默認參數。不支持變長參數表, 但可以用傳送數組的方法來實現。
3、 關聯參數
默認情況函數參數是傳值方式。如果你允許函數修改傳入參數的值,你可以使用變量參數。
如果你希望函數的一個形式參數始終是變量參數,你可以在函數定義時給該形式參數加(&)前綴:
function foo( &$bar ) {
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo( $str );
echo $str; // outputs 'This is a string, and something extra.'
如果要傳遞一個可變參數給默認的函數(其形式參數不是變參方式),你可以在調用函數時給實際參數加(&)前綴:
function foo( $bar ) {
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo( $str );
echo $str; // outputs 'This is a string, '
foo( &$str );
echo $str; // outputs 'This is a string, and something extra.'
4、 默認值
函數可以定義 C++ 風格的默認值,如下:
function makecoffee( $type = "cappucino" ) {
echo "Making a cup of $type.\n";
}
echo makecoffee();
echo makecoffee( "espresso" );
上邊這段代碼的輸出是:
Making a cup of cappucino.
Making a cup of espresso.
注意,當使用默認參數時,所有有默認值的參數應在無默認值的參數的后邊定義;否則,將不會按所想的那樣工作。
5、CLASS(類)
類是一系列變量和函數的集合。類用以下語法定義:
class Cart {
var $items; // Items in our shopping cart
// Add $num articles of $artnr to the cart
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} else {
return false;
}
}
}
?>
上面定義了一個叫Cart 的類,其中包括一個關聯數組和兩個用來從cart中增加和刪除項目的函數。
類是實際變量的原始模型。你要通過new 操作符來建立一個所需類型的變量。
$cart = new Cart;
$cart->add_item("10", 1);
這建立起一個 Cart類的對象$cart。該對象的函數add_item()被調用來給第10項加 1。
類可以從其他的類擴充得到。擴充或派生出來的類擁有基類的所有變量和函數及你在擴充定義中所定義的東西。這要使用 extends 關鍵字。
class Named_Cart extends Cart {
var $owner;
function set_owner($name) {
$this->owner = $name;
}
}
這里定義了一個名為 Named_Cart 的類它繼承了 Cart類所有變量和函數并增加了一個變量 $owner和一個函數 set_owner()。 你建立的 named_cart 類的變量現在就能設置carts 的 owner了。在named_cart變量中你仍然可以使用一般的 cart函數:
$ncart = new Named_Cart; // Create a named cart
$ncart->set_owner("kris"); // Name that cart
print $ncart->owner; // print the cart owners name
$ncart->add_item("10", 1); // (inherited functionality from cart)
函數中的變量 $this 意思是當前的對象。你需要使用 $this->something 的形式來存取所有當前對象的變量或函數。
類中的構造器是你建立某種類的新變量時自動被調用的函數。類中和類名一樣的函數就是構造器。
class Auto_Cart extends Cart {
function Auto_Cart() {
$this->add_item("10", 1);
}
}
這里定義一個類 Auto_Cart ,它給 Cart類加了一個每次new操作時設置項目10進行變量初始化的構造器。構造器也可以有參數,這些參數是可選的,這種特點也使得其十分有用。
class Constructor_Cart {
function Constructor_Cart($item = "10", $num = 1) {
$this->add_item($item, $num);
}
}
// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;
// Shop for real...
$different_cart = new Constructor_Cart("20", 17);