Search code examples
phpvar-dump

How to access variable with dot


Here is part of my var_dump($GLOBALS['pagination']);:

object(VmPagination)[376]
  ...
  public 'pages.total' => float 4
  public 'pages.current' => float 1
  public 'pages.start' => int 1
  public 'pages.stop' => float 4
  ...

Is there any ideas how can I access pages.total and other three variables with dot? I tried $GLOBALS['pagination']->pages.current, but it's doesn't work.


Solution

  • Use the {} notation:

    $foo->{'bar.baz'} = 'qux';
    

    e.g:

    php > $x = new StdClass();
    php > $x->{'foo.bar'} = 'baz';
    php > var_dump($x);
    object(stdClass)#1 (1) {
      ["foo.bar"]=>
      string(3) "baz"
    }
    php > echo $x->foo.bar;
    PHP Notice:  Undefined property: stdClass::$foo in php shell code on line 1
    PHP Notice:  Use of undefined constant bar - assumed 'bar' in php shell code on line 1
    bar
    php > echo $x->{'foo.bar'};
    baz