如何访问 std 对象中的受保护变量



你好,我在 std 对象中有一个受保护变量的结果。当我这样做时print_r结果是

            libphonenumberPhoneNumber Object
(
    [countryCode:protected] => 91
    [nationalNumber:protected] => 321476551
    [extension:protected] => 
    [italianLeadingZero:protected] => 
    [rawInput:protected] => 
    [countryCodeSource:protected] => 4
    [preferredDomesticCarrierCode:protected] => 
    [hasNumberOfLeadingZeros:protected] => 
    [numberOfLeadingZeros:protected] => 1
)

我想访问可变的国家/地区代码。当我这样做时

echo $phoneNumberObject->countryCode;

它说

Cannot access protected property libphonenumberPhoneNumber::$countryCode in...

提前致谢

除非您使用 Reflection 或正常过程的其他旁路 - 否则任何类中的受保护变量和私有变量都不能在类之外访问。 通常,尽管任何API都会提供各种方法来访问数据。

所以通常你会在PhoneNumber课上找到类似getCountryCode()的东西。

如果没有这样的方法 - 那么这可能表明该变量不是您应该访问的内容,并且更有可能是内部状态而不是有用的值。

最新更新