Funky PHP Namespace Not Resolving

I've got an infuriating problem with PHP namespaces that should be incredibly simple, but has consumed about a days worth of work.

Here's the layout:

The directory structure:

/site

    /classes
            /cache.php
    /index.php

cache.php is as follows:

<?
    namespace {
        class Cache {...}
    }
?>

index.php consists of:

<?php
    namespace {
        \Cache::get()->activate();
    }
?>

I've tried about a million different ways of namespacing this, but index.php never has visibility to the Cache class. Surely this should take about 5 seconds to figure out, right?

Thanks!

EDIT:

Thanks for the suggestions!

I've created a cachetest.php page with the following code:

namespace {
    set_include_path(get_include_path() . PATH_SEPARATOR . './classes');
    set_include_path(get_include_path() . PATH_SEPARATOR . './libraries/oauth-php');
    spl_autoload_register(function ($name) {
        spl_autoload($name);
    });

    $cache = \Cache::get();
}

Still crapping out with an "Fatal error: Class 'Cache' not found" error though. So bizarre given that the autoloader will register other classes in the same directory without a problem.

Answers


You have two problems.

  1. You have no namespaces at all here. That's how you declare a namespace: http://www.php.net/manual/en/language.namespaces.definition.php

  2. You have no autoloader in place. You must either require_once('../classes/Cache.php); or make a (preferably PSR-compliant) autoloader script and register it with spl_autoload_register().


Turns out the answer was that PHP on windows is extremely sensitive to odd control characters or something. I eventually solved the class resolution problem by commenting out every line in the file, then carefully cutting and pasting them back in--which seemed to eliminate whatever hidden garbage was keeping the autoloader from doing it's stuff. Pretty awful.


Need Your Help

apply user defined function to each element of a dataframe

r dataframe sapply

I was not able to find the solution to this easy question in R. How to apply a "user defined" function to each element (cell) of a dataframe with out looping?