Laravel 4:鉴于此回复,我如何访问"数据"可变/键?

时间:2015-04-21 14:59:25

标签: php laravel

我有一个单元测试,其中一个函数执行以下操作:

$response = Response::json('success');

var_dump($response);给出:

object(Illuminate\Http\JsonResponse)[184]
  protected 'jsonOptions' => int 0
  protected 'data' => string '"success"' (length=9)
  protected 'callback' => null
  protected 'encodingOptions' => int 15
  public 'headers' => 
    object(Symfony\Component\HttpFoundation\ResponseHeaderBag)[192]
      protected 'computedCacheControl' => 
        array (size=1)
          'no-cache' => boolean true
      protected 'cookies' => 
        array (size=0)
          empty
      protected 'headerNames' => 
        array (size=3)
          'cache-control' => string 'Cache-Control' (length=13)
          'date' => string 'Date' (length=4)
          'content-type' => string 'Content-Type' (length=12)
      protected 'headers' => 
        array (size=3)
          'cache-control' => 
            array (size=1)
              ...
          'date' => 
            array (size=1)
              ...
          'content-type' => 
            array (size=1)
              ...
      protected 'cacheControl' => 
        array (size=0)
          empty
  protected 'content' => string '"success"' (length=9)
  protected 'version' => string '1.0' (length=3)
  protected 'statusCode' => int 200
  protected 'statusText' => string 'OK' (length=2)
  protected 'charset' => null

可以访问data密钥吗?

protected 'data' => string '"success"' (length=9)

因为它受保护,$response->data无效

1 个答案:

答案 0 :(得分:1)

$response->getData();

顺便说一下,将来你可以通过转储所有对象方法来自己回答这些问题:

dd(get_class_methods($response));