简体   繁体   中英

Why can I only access the first protected property in this PHP object?

This works:

public function import_tickets($user = null) {
    $tickets = kyTicket::getAll(
        kyDepartment::getAll(),
        kyTicketStatus::getAll(),
        array(),
        kyUser::getAll()
    );
    $reflect = new ReflectionClass($tickets);
    $ts = $reflect->getProperty('objects');
    $ts->setAccessible(true);
    $content = $ts->getValue($tickets);
    $output = '';
    foreach ( $content as $c ) {
        $output .= $c->id . "\n";
    }
    print_r($output);
}

OUTPUT:

[root@matthewharris External]# php test.php 

1
2
3
4
5
6

I am trying to access the display_id from the following object:

[4] => kyTicket Object
    (
        [id:protected] => 5
        [flag_type:protected] => 0
        [display_id:protected] => RXH-123-45678
        [department_id:protected] => 5
        [status_id:protected] => 3
        [priority_id:protected] => 1

But when I do I get the following error:

[02-Oct-2014 12:14:29] PHP   3. kyObjectBase->__get($api_field_name = 'display_id') /var/www/html/site/public_html/inc/QA/External/test.php:43
[02-Oct-2014 12:14:29] PHP   4. trigger_error('Undefined property: kyTicket::$display_id', 1024) /var/www/html/site/public_html/inc/QA/External/api-kayako/kyObjectBase.php:573
[02-Oct-2014 12:14:29] PHP Notice:  Undefined property: kyTicket::$display_id in /var/www/html/site/public_html/inc/QA/External/api-kayako/kyObjectBase.php on line 573

Why can I access id without issue but display_id will not be captured?

$reflect = new ReflectionClass($tickets); 
$reflectionProperty = $reflect->getProperty('objects');
$reflectionProperty->setAccessible(true); 
$objects = $reflectionProperty->getValue($tickets);

// you need object "tickets" - property "objects" for the iteration

foreach($objects as $object) {
   listProperties($object);
}

function listProperties($object)
{
    echo 'Properties for Object: ' . get_class($object);

    $reflect = new ReflectionClass($object); 

    $properties = $reflect->getProperties(
        ReflectionProperty::IS_PUBLIC + 
        ReflectionProperty::IS_PROTECTED
    );

    foreach ($properties as $prop) {
        echo $prop->getName() . "\n";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM