Search code examples
phpnamespacesapc

Namespacing and APC fails


I'm trying to use APC with PHP. I've copied the code from the PHP manual for storing arrays in APC; and it works great, as I would expect.

My problem arises when I introduce a namespace when it fails.

<?php
namespace tester;

$objs = array();
$objs[] = "123";
$objs[] = "123";
$objs[] = "123";

apc_store('tester:objs', new ArrayObject($objs),60);
$tmp = apc_fetch('tester:objs');
print_r($tmp -> getArrayCopy());
exit;

Solution

  • Ahhhhh! I knew it would be something simple! I simple forgot to declare ArrayObject as a global namespace class. Adding a slash in front of ArrayObject worked. Silly me! :(

    Working code:

    <?php
    namespace tester;
    
    $objs = array();
    $objs[] = "123";
    $objs[] = "123";
    $objs[] = "123";
    
    apc_store('tester:objs', new \ArrayObject($objs),60);
    $tmp = apc_fetch('tester:objs');
    print_r($tmp -> getArrayCopy());
    exit;