一、简介
在 PHP 中,类的变量成员被称为属性或字段,属性开头至少需要一个修饰符(例如访问控制、静态关键字或 PHP 8.1.0 及以上版本支持的 readonly 修饰符),除了 readonly 属性外,所有修饰符都是可选的。从 PHP 7.4 开始,一个类型声明可以紧随修饰符后面,随后跟随一个普通的变量声明。属性中的变量可以被初始化,但初始化的值必须是常量值。
注意:
- 另一种过时的声明类属性的方法是使用 var 关键字,而不是使用修饰符。
- 没有声明访问控制(可见性)修饰符的属性将默认声明为 public。
- 在类的成员方法里面,可以用 ->(对象运算符):$this->property(其中 property 是该属性名)这种方式来访问非静态属性。静态属性则是用 ::(双冒号):self::$property 来访问。
二、属性声明
当一个方法在类定义内部被调用时,有一个可用的伪变量 $this。$this 是一个到主叫对象的引用(通常是该方法所从属的对象,但如果是从第二个对象静态调用时也可能是另一个对象)。
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a>
public $var1 = 'hello ' . 'world';
public $var4 = self::myStaticMethod();
public $var6 = myConstant;
public $var7 = array(true, false);
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a>
class SimpleClass
{
public $var1 = 'hello ' . 'world';
public $var2 = <<<EOD
hello world
EOD;
public $var3 = 1+2;
// 错误的属性声明
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
// 正确的属性声明
public $var6 = myConstant;
public $var7 = array(true, false);
public $var8 = <<<'EOD'
hello world
EOD;
// 没有访问控制修饰符:
static $var9;
readonly int $var10;
}
?>
<?php
class SimpleClass
{
public $var1 = 'hello ' . 'world';
public $var2 = <<<EOD
hello world
EOD;
public $var3 = 1+2;
// 错误的属性声明
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
// 正确的属性声明
public $var6 = myConstant;
public $var7 = array(true, false);
public $var8 = <<<'EOD'
hello world
EOD;
// 没有访问控制修饰符:
static $var9;
readonly int $var10;
}
?>
三、类型声明
从 PHP 7.4.0 开始,属性定义可以包含类型声明,但 callable 除外。
1、类型声明
public function __construct(int $id, ?string $name)
$user = new User(1234, null);
<?php
class User
{
public int $id;
public ?string $name;
public function __construct(int $id, ?string $name)
{
$this->id = $id;
$this->name = $name;
}
}
$user = new User(1234, null);
var_dump($user->id);
var_dump($user->name);
?>
<?php
class User
{
public int $id;
public ?string $name;
public function __construct(int $id, ?string $name)
{
$this->id = $id;
$this->name = $name;
}
}
$user = new User(1234, null);
var_dump($user->id);
var_dump($user->name);
?>
以上示例会输出:
int(1234)
NULL
类型属性必须在访问前初始化,否则会抛出 Error 。
2、访问属性
public int $numberOfSides;
public function setNumberOfSides(int $numberOfSides): void
$this->numberOfSides = $numberOfSides;
public function setName(string $name): void
public function getNumberOfSides(): int
return $this->numberOfSides;
public function getName(): string
$triangle->setName("triangle");
$triangle->setNumberofSides(3);
var_dump($triangle->getName());
var_dump($triangle->getNumberOfSides());
$circle->setName("circle");
var_dump($circle->getName());
var_dump($circle->getNumberOfSides());
<?php
class Shape
{
public int $numberOfSides;
public string $name;
public function setNumberOfSides(int $numberOfSides): void
{
$this->numberOfSides = $numberOfSides;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getNumberOfSides(): int
{
return $this->numberOfSides;
}
public function getName(): string
{
return $this->name;
}
}
$triangle = new Shape();
$triangle->setName("triangle");
$triangle->setNumberofSides(3);
var_dump($triangle->getName());
var_dump($triangle->getNumberOfSides());
$circle = new Shape();
$circle->setName("circle");
var_dump($circle->getName());
var_dump($circle->getNumberOfSides());
?>
<?php
class Shape
{
public int $numberOfSides;
public string $name;
public function setNumberOfSides(int $numberOfSides): void
{
$this->numberOfSides = $numberOfSides;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getNumberOfSides(): int
{
return $this->numberOfSides;
}
public function getName(): string
{
return $this->name;
}
}
$triangle = new Shape();
$triangle->setName("triangle");
$triangle->setNumberofSides(3);
var_dump($triangle->getName());
var_dump($triangle->getNumberOfSides());
$circle = new Shape();
$circle->setName("circle");
var_dump($circle->getName());
var_dump($circle->getNumberOfSides());
?>
以上示例会输出:
Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization
string(8) "triangle"
int(3)
string(6) "circle"
Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization
string(8) "triangle"
int(3)
string(6) "circle"
Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization
四、只读属性
自 PHP 8.1.0 起,可以使用 readonly 修饰符声明属性,防止初始化后修改属性。
1、只读属性
public readonly string $prop;
public function __construct(string $prop) {
$test = new Test("foobar");
var_dump($test->prop); // string(6) "foobar"
// Error: Cannot modify readonly property Test::$prop
<?php
class Test {
public readonly string $prop;
public function __construct(string $prop) {
// 初始化正常。
$this->prop = $prop;
}
}
$test = new Test("foobar");
// 读取正常。
var_dump($test->prop); // string(6) "foobar"
// 再赋值异常。分配的值是否相同并不重要。
$test->prop = "foobar";
// Error: Cannot modify readonly property Test::$prop
?>
<?php
class Test {
public readonly string $prop;
public function __construct(string $prop) {
// 初始化正常。
$this->prop = $prop;
}
}
$test = new Test("foobar");
// 读取正常。
var_dump($test->prop); // string(6) "foobar"
// 再赋值异常。分配的值是否相同并不重要。
$test->prop = "foobar";
// Error: Cannot modify readonly property Test::$prop
?>
readonly 修饰符只能应用于类型化属性。可以使用 Mixed 类型创建没有类型约束的只读属性。
注意:
- 不支持对静态属性只读。
- 只读属性只能初始化一次,并且只能从声明它的作用域内初始化。对属性的任何赋值和修改都会导致 Error 异常。
2、初始化只读属性异常
public readonly string $prop;
// Error: Cannot initialize readonly property Test1::$prop from global scope
<?php
class Test1 {
public readonly string $prop;
}
$test1 = new Test1;
// 私有作用域之外异常初始化。
$test1->prop = "foobar";
// Error: Cannot initialize readonly property Test1::$prop from global scope
?>
<?php
class Test1 {
public readonly string $prop;
}
$test1 = new Test1;
// 私有作用域之外异常初始化。
$test1->prop = "foobar";
// Error: Cannot initialize readonly property Test1::$prop from global scope
?>
禁止在只读属性上指定默认值,因为具有默认值的只读属性等同于常量,因此不是特别有用。
// Fatal error: Readonly property Test::$prop cannot have default value
public readonly int $prop = 42;
<?php
class Test {
// Fatal error: Readonly property Test::$prop cannot have default value
public readonly int $prop = 42;
}
?>
<?php
class Test {
// Fatal error: Readonly property Test::$prop cannot have default value
public readonly int $prop = 42;
}
?>
只读属性一旦初始化就不能 unset()。但可以在初始化之前从声明属性的作用域中取消只读属性。
修改不一定是简单的赋值,以下所有行为也会导致 Error 异常:
public function __construct(
public readonly int $i = 0,
public readonly array $ary = [],
foreach ($test as &$prop);
<?php
class Test {
public function __construct(
public readonly int $i = 0,
public readonly array $ary = [],
) {}
}
$test = new Test;
$test->i += 1;
$test->i++;
++$test->i;
$test->ary[] = 1;
$test->ary[0][] = 1;
$ref =& $test->i;
$test->i =& $ref;
byRef($test->i);
foreach ($test as &$prop);
?>
<?php
class Test {
public function __construct(
public readonly int $i = 0,
public readonly array $ary = [],
) {}
}
$test = new Test;
$test->i += 1;
$test->i++;
++$test->i;
$test->ary[] = 1;
$test->ary[0][] = 1;
$ref =& $test->i;
$test->i =& $ref;
byRef($test->i);
foreach ($test as &$prop);
?>
然而,只读属性并不会妨碍内部可变性。存储在只读属性中的对象(或资源)仍然可以在内部修改:
public function __construct(public readonly object $obj) {}
$test = new Test(new stdClass);
$test->obj = new stdClass;
<?php
class Test {
public function __construct(public readonly object $obj) {}
}
$test = new Test(new stdClass);
// 内部可变正常。
$test->obj->foo = 1;
// 赋值异常。
$test->obj = new stdClass;
?>
<?php
class Test {
public function __construct(public readonly object $obj) {}
}
$test = new Test(new stdClass);
// 内部可变正常。
$test->obj->foo = 1;
// 赋值异常。
$test->obj = new stdClass;
?>
五、动态属性
如果尝试在 object 上赋值不存在的属性,PHP 将会自动创建相应的属性。动态创建的属性将仅能在此类实例上使用。
注意:自 PHP 8.2.0 起弃用动态属性。建议更改为属性声明。要处理任意属性名称,类应该实现魔术方法 __get() 和 __set()。最后可以使用 #[\AllowDynamicProperties] 注解标记此类。