Php 如何访问std对象中的受保护变量
您好,我在std对象中有一个受保护变量的结果。当我打印时,结果是Php 如何访问std对象中的受保护变量,php,Php,您好,我在std对象中有一个受保护变量的结果。当我打印时,结果是 libphonenumber\PhoneNumber Object ( [countryCode:protected] => 91 [nationalNumber:protected] => 321476551 [extension:protected] => [italianLeadingZero:protected] => [rawIn
libphonenumber\PhoneNumber 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 libphonenumber\PhoneNumber::$countryCode in...
提前感谢除非您使用反射或正常流程的其他步骤,否则任何类中的受保护和私有变量都不能在类外访问。通常情况下,任何API都会提供各种方法来访问数据 因此,通常您会在
PhoneNumber
类中找到类似getCountryCode()
的内容
如果没有这样的方法,那么这可能表明变量不是您应该访问的对象,更可能是一个内部状态,而不是一个有用的值。噢,非常感谢。您的解决方案有效请将此作为答案发布