0

how would I go about getting around the "protected" so I can output the data.

tabs\api\property\Property Object ( 
    [id:protected] => 90_4_HH 
    [propertyRef:protected] => 90_4 
    [brandCode:protected] => HH 
    [url:protected] => http://hh.api.carltonsoftware.co.uk/property/90_4_HH
    [accountingBrand:protected] => HH 
    [slug:protected] => 90-4-hh 
    [name:protected] => Carreg Lwyd Farmhouse 
    [address:protected] => tabs\api\core\Address Object ( 
        [addr1:protected] => Port Eynon 
        [addr2:protected] => 
        [town:protected] => Gower 
        [county:protected] => Swansea 
        [postcode:protected] => SA3 1NN 
        [country:protected] => GB 
    ) 
    [changeOverDay:protected] => Saturday 
    [calendar:protected] => http://hh.api.carltonsoftware.co.uk/property/90_4_HH/calendar 
    [booking:protected] => http://hh.api.carltonsoftware.co.uk/booking 
    [pets:protected] => 
    [promote:protected] => 
    [smoking:protected] => 
    [shortlist:protected] => 
    [accommodates:protected] => 12 
    [rating:protected] => 5 
    [bedrooms:protected] => 6 
    [images:protected] => Array ( 
        [90_4p1190276.jpg?APIKEY=homefromhome&hash=31b3f7b1b377184e8cb8fb64d434a11a4c3446c1091535ef6db4e119689a6372] => tabs\api\property\Image Object ( 
            [filename:protected] => 90_4p1190276.jpg?APIKEY=homefromhome&hash=31b3f7b1b377184e8cb8fb64d434a11a4c3446c1091535ef6db4e119689a6372 
            [title:protected] => Carreg Lwyd Farmhouse, Port Eynon 
            [alt:protected] => The Lounge 
            [url:protected] => http://hh.api.carltonsoftware.co.uk/image/normal/1000x750/90_4p1190276.jpg 
            [height:protected] => 750 
            [width:protected] => 1000 
            [apiPath:protected] => http://hh.api.carltonsoftware.co.uk 
        ) 
        [90_4img_4819.jpg?APIKEY=homefromhome&hash=31b3f7b1b377184e8cb8fb64d434a11a4c3446c1091535ef6db4e119689a6372] => tabs\api\property\Image Object ( 

The dump:

foreach ($properties as $property) {
echo sprintf('<p class="listit">%s</p>', $property);
print_r($property);
}
4

3 回答 3

1

Your title implies that you want to make -the class (not object)- public, as opposed to "internal"/etc.. All classes are public in PHP.

Your question says that you want to get around method/property scope (disagrees with title). You didn't say what you've tried. You also didn't indicate whether this is a class you developed or not. If you have control over it, add a freaking method or two to allow you to get the data. If you don't, then inspect the methods, and/or attempt reflection. These answers apply to every OOP language in existence.

Before asking other people, read the documentation.

http://www.php.net/manual/en/language.oop5.basic.php

于 2013-09-04T20:36:33.180 回答
1

I did not fully understand your question, but if you want to access protected properties from outside the class, you have to use Reflection:

$reflObj = new ReflectionObject($property);
$props = $reflObj->getProperties(ReflectionProperty::IS_PROTECTED);

foreach ($props as $prop) {
  $prop->setAccessible(true);
  echo $prop->getName() . ":" . $prop->getValue($property), "\n";
}

Sample for outputting the address:

$reflObj = new ReflectionObject($property);
$addrProp = $reflObj->getProperty('address');

$addrProp->setAccessible(true);
echo $addrProp->getValue($property);
于 2013-09-04T20:40:12.907 回答
0

Answer to the question posed by the title is that all classes are public.

What you are asking is how to access protected member variables.

Taken from here (http://ajmm.org/2011/06/using-php-reflection-to-read-a-protected-property/), this is an example of how to do this:

public static function getReflectedPropertyValue($class, $propertyName)
{
    $reflectedClass = new ReflectionClass($class);
    $property = $reflectedClass->getProperty($propertyName);
    $property->setAccessible(true);

    return $property->getValue($class);
}

...

getReflectedPropertyValue($yourObject, 'protectedProperty');

That said, the question is why you want to do this. Members are marked protected specifically to prevent you from doing this. If you have access to the source code that defines this other class, then it might make more sense to either change these members to "public" or (better) to provide a getXYZ() method for whichever properties you want to access.

于 2013-09-04T20:45:07.757 回答