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