user1hjgjhgjhggjhg
user1hjgjhgjhggjhg

Reputation: 1327

How to access protected variable which is in std object

Hello I have a result in protected variables in std object. When I do print_r the result is

            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
)

I want to access the variable country Code. When I do this

echo $phoneNumberObject->countryCode;

It says

Cannot access protected property libphonenumber\PhoneNumber::$countryCode in...

Thanks in advance

Upvotes: 0

Views: 87

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

Unless you use Reflection or other sidesteps of the normal process - protected and private variables in any class cannot be accessed outside of the class. Normally though any API will provide various methods to access data.

So usually you will find something like getCountryCode() in the PhoneNumber class.

If there isn't such a method - then this may indicate the variable isn't something you should be accessing and is more likely an internal state rather than a useful value.

Upvotes: 1

Related Questions