john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

access object public and private variables reflection kohana array

$result_object_properties_array = get_object_vars($result);

foreach(array_keys($result_object_properties_array) as $key=>$value) {
  kohana::log("info", "Exporter response: ".$key."=".$value);
}


//presuming you know what private variables you wish to access:

$class_object = new ExampleClass('value for some_private_variable');
$reflectionClass = new ReflectionClass( get_class( $rest ) );
$reflectionProperty = $myClassReflection->getProperty( 'some_private_variable' );
$reflectionProperty->setAccessible( true );
$reflectionPropertyValue = $reflectionProperty->getValue( $rest );
$this->assertEquals('value for some_private_variable', $reflectionPropertyValue);



     $rest = new S3Request('HEAD', $this->bucket, $uploadFile, '127.0.0.1');

      $class_vars = get_class_vars( get_class( $rest ) );   //public variables
      foreach( $class_vars as $name => $value ) {
        print "REST REQUEST: $name : $value";
      }


      //example of a private array and a private string variable
      $rest = new S3Request( 'HEAD', $this->bucket, $uploadFile );
      $myClassReflection = new ReflectionClass( get_class( $rest ) );
      $restProperty = $myClassReflection->getProperty( 'headers' );
      $restProperty->setAccessible( true );
      $restPropertyValue = $restProperty->getValue( $rest );

      kohana::log( 'info', "HEADERS DATE: " . $restPropertyValue['Date'] );
      kohana::log( 'info', "HEADERS HOST: " . $restPropertyValue['Host'] );
      kohana::log( 'info', "ArraySize: " . sizeof( $restPropertyValue ) );

      kohana::log( 'info', "ArrayContents: " . implode( ', ', $restPropertyValue)  );



      $restProperty = $myClassReflection->getProperty( 'resource' );
      $restProperty->setAccessible( true );
      $restPropertyValue = $restProperty->getValue( $rest );
      kohana::log( 'info', "REST RESOURCE: " . $restPropertyValue );



    //workaround for Kohana bug for not displaying arrays
    $mykeys = array_keys( $config );
    $configString = implode( ',' , $mykeys );
    kohana::log("error", $configString );



protected static function getMethod($name) {
  $class = new ReflectionClass('MyClass');
  $method = $class->getMethod($name);
  $method->setAccessible(true);
  return $method;
}

public function testFoo() {
  $foo = self::getMethod('foo');
  $obj = new MyClass();
  $foo->invokeArgs($obj, array(...));
  ...
}

  • « selenium phantomjs SeleniumTest javascript injection iframes
  • eclipse java executable jar list contents »

Published

Dec 11, 2014

Category

php

~164 words

Tags

  • access 8
  • and 29
  • array 16
  • kohana 2
  • object 7
  • php 82
  • private 2
  • public 1
  • reflection 1
  • variables 5