首頁  >  文章  >  後端開發  >  PHP 參考資料

PHP 參考資料

WBOY
WBOY原創
2024-08-29 12:49:00868瀏覽

PHP 引用是符號表別名,透過它可以透過不同的名稱存取一個變數的內容。明確定義的引用變數前面需要加上“&”符號。 PHP 引用的功能可以用 Window 的捷徑類比來解釋。 PHP 引用可以透過多種方式在 PHP 程式設計中定義。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

建立 PHP 引用的方法

建立 PHP 引用最常用的方法討論如下:

1.使用關鍵字「global」

在方法中,可以在引用的變數之前使用關鍵字「global」來建立引用。將引用宣告為全域變數會將變數新增至 $GLOBAL 陣列中,並使使用者能夠在函數範圍內存取全域變數。基本上有兩種方法可以將 PHP 引用定義為全域變量,例如:

function Function_name() {
global $globalVar;
}
OR
function Function_name() {
$globalVar =& $GLOBALS["globalVar"];
}

範例

下面的程式碼片段旨在演示同一變數的值在局部範圍和全域範圍內的差異。

<?php
function functionname() {
$inputvar = "within function scope";
echo '$inputvar in global scope: ' . $GLOBALS["inputvar"] . "\n";
echo '$inputvar in current scope: ' . $inputvar . "\n";
}
$inputvar = "Outside function scope";
$othervar= $GLOBALS["inputvar"]; //Creating inputvar in GLOBAL array
functionname();
echo '$othervar : ' . $othervar . "\n";
?>

輸出

Othervar 是 GLOBAL 陣列中 inputvar 的參考集。它不會綁定到函數局部作用域中定義的 inputvar 變數。

PHP 參考資料

2.使用 $this 變數

‘$this’變數是函數預設的物件引用,其中$this變數被引用。

範例

下面的程式碼示範如何使用 $this 變數來存取所選類別物件中任何類別屬性的值。

<?php
class Thisclass {
var $clsproperty = 300;
function classmethod() {
$this->clsproperty = 500; // $this is a reference to the object
}
}
$clsObject = new Thisclass();
$clsObject->classmethod();
echo "The displayed value is: ". $clsObject->clsproperty;
//display the value updated using $this property
?>

輸出

根據使用 $this 變數設定的值顯示 clsproperty 的值。

PHP 參考資料

3.傳遞一個物件

在PHP程式設計中,對類別物件執行的任何操作,例如分配、傳回或傳遞等;操作總是參考物件而不是其副本來執行。

建立 PHP 物件引用的標準語法如下:

class ClassName {
//Body of the class
}
$classObj1 = new ClassName ();
$classObj2= $classObj1;

這裡classObj2物件引用了classObj1所包含的相同內容。

範例

下面的程式碼片段旨在為實際物件建立引用物件並存取其屬性。

<?php
class Costume {
// Declaring the class properties
public $name;
public $color;
// Declaring the class methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
//Creating the object
$constume1 = new Costume();
$constume1->set_name('Superman');
$constume1->set_color('Blue and Red');
//Creating the object reference
$constume2=$constume1;
echo "Costume1 Name: " . $constume1->get_name();
echo "\n";
echo "Costume1 Color: " .  $constume1->get_color();
echo "\n";
echo "\n";
echo "Costume2 Name: " . $constume2->get_name();
echo "\n";
echo "Costume2 Color: " .  $constume2->get_color();
?>

輸出

參考物件 Costume2 引用與實際物件 Costume1 的屬性名稱和顏色中攜帶的相同值。

PHP 參考資料

PHP程式設計的不同操作

在 PHP 程式設計中,不同的操作是透過 PHP 引用來執行的。下面的會議討論了一些主要操作:

1.透過引用傳遞

為了使函數能夠修改定義在其範圍之外的變量,該值需要透過其參考傳遞給函數。

範例

下面的程式碼片段使用變數的參考更改了被呼叫函數範圍之外定義的變數的值。

<?php
function Afunction(&$input) //passing the input argument by reference
{
$input*=10;
}
$outVar=5;
echo "Before the function is called: ".$outVar;
echo "\n";
Afunction($outVar);
echo "After the function is called: ".$outVar;
?>

輸出

變數 outvar 的值由函數 AFunction() 變更。

PHP 參考資料

2.回參考文獻

此操作使呼叫函數能夠找出要綁定參考的變數。建議僅在有任何技術要求時使用,否則不會增加程式的效能。

範例

下面的程式碼片段旨在將函數父函數的傳回值作為定義的類別父類別的參考傳遞。

<?php
class parentclass {
public $parentvar = "I am set at parent class";
public function &parentfunction()
{
return $this->parentvar;
}
}
$parentobj = new parentclass;
$newvar = &$parentobj->parentfunction();
echo $newvar;
echo "\n";
$parentobj->parentvar= "I am set outside of the class";
echo $newvar;
?>

輸出

PHP 參考資料

3.取消設定 PHP 參考

使用者可以使用 unset() 方法來打破變數和參考之間的綁定。

範例

下面的程式碼片段示範如何使用 unset() 方法來解除引用變數firstinput與secondinput的綁定。

<?php
$firstinput = "I am first input";
$secondinput =& $firstinput;
echo "First input: ". $firstinput;
echo"\n";
echo "Second input: " . $secondinput;
unset($firstinput);
echo"\n";
echo"\n";
echo "After unsetting the reference: ";
echo"\n";
$firstinput = "I am set to second input";
echo"\n";
echo "First input: ". $firstinput;
echo"\n";
echo "Second input: " . $secondinput;
?>

輸出

PHP 參考資料

結論

PHP 引用是 PHP 腳本包含的重要功能。 PHP 引用不是指針,因為它可以用「C」來描述,它也佔用記憶體來建立重複元素。相反,PHP 引用只是引用實際變數內容的不同別名。如果 PHP 中的物件需要複製對象,可以使用關鍵字「clone」來完成。

以上是PHP 參考資料的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 取得類型()下一篇:PHP 取得類型()