大网数据广告活动
PHP 语言与运行手册

ReflectionProperty::getSettableType · Returns the parameter type of a setter hook

· 52CE 我爱测

ReflectionProperty::getSettableType — Returns the parameter type of a setter hook

ReflectionProperty::getSettableType

(PHP 8 >= 8.4.0)

ReflectionProperty::getSettableType — Returns the parameter type of a setter hook

Description

public function ReflectionProperty::getSettableType(): ?ReflectionType

Returns the parameter type of a set hook. If no set hook is defined, it behaves identically to ReflectionProperty::getType().

Parameters

This function has no parameters.

Return Values

  • This method returns an instance of ReflectionType that matches the settable type for the property.
  • If there is a set hook that defines an explicit type, that will be returned.
  • If the hook does not specify a type, or it does not exist, the property type will be returned, identically to ReflectionProperty::getType(). This value may be null if the property is untyped.
  • If the property is virtual and has no set hook, a ReflectionType instance for never will be returned.

Examples

Example #1 ReflectionProperty::getSettableType() example

<?php

class Example
{
    public string $basic {
        set => strtolower($value);
    }

    public string $wider {
        set(string|Stringable $value) => (string) $value;
    }

    public string $virtual {
        get => 'Do not change this';
    }

    public $untyped = 'silly';
}

$rClass = new \ReflectionClass(Example::class);

var_dump($rClass->getProperty('basic')->getSettableType());
var_dump($rClass->getProperty('wider')->getSettableType());
var_dump($rClass->getProperty('virtual')->getSettableType());
var_dump($rClass->getProperty('untyped')->getSettableType());

?>

The above example will output:

object(ReflectionNamedType)#3 (0) {
}
object(ReflectionUnionType)#2 (0) {
}
object(ReflectionNamedType)#3 (0) {
}
NULL

See Also

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。