首页  >  文章  >  后端开发  >  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 gettype()下一篇:PHP count