1

I am experimenting with PHP namespacing within Worpdress plugins. The plugin includes a loader file which sets up the spl_autoload functionality and then instantiates a different controller depending on whether the current page request 'is_admin()' or not.

[WP_PLUGIN_DIR]/lhes_experiment/loader.php

use lhes\system\controllers as sys;

set_include_path('./wp-content/plugins/lhes_experiment/' . PATH_SEPARATOR .     get_include_path());
spl_autoload_extensions(".php"); // comma-separated list
spl_autoload_register( 'spl_autoload' );

$config = array( ... );

if( is_admin() ){
   error_log( var_export( new sys\experiment( ), true ), 0 );
}else{
   error_log( var_export( new sys\experiment( ), true ), 0 );
}

[WP_PLUGIN_DIR]/lhes_experiment/lhes/system/controllers/experiment.php

namespace lhes\system\controllers;
   class experiment extends test{
      public $property;
      function __construct( ){
         $this->property = 'owls';
      }
   }

[WP_PLUGIN_DIR]/lhes_experiment/lhes/system/controllers/test.php

namespace lhes\system\controllers;
   class test{
      public $test;
      function __construct( ){
         $this->test = 'hi';
      }
   }

When the request is not and admin page, the code runs as expected and the error log shows:

[23-Jan-2013 12:55:27 UTC] lhes\system\controllers\experiment::__set_state(array(
   'property' => 'owls',
   'test' => 'hi',
))

However, when the request is to an admin page the exact same code does not work:

[23-Jan-2013 12:56:17 UTC] PHP Fatal error:  Class 'lhes\system\controllers\test' not found in C:\Program Files (x86)\Zend\Apache2\htdocs\wp-content\plugins\lhes_experiment\lhes\system\controllers\experiment.php on line 4

I've spent many an hour googling the problem but can't seem to find anything. If anyone has any ideas as to why this might be happening I would really appreciate some pointers.

1 Answer 1

1

Your set_include_path is wrong.

Instead use:

set_include_path(plugin_dir_path(__FILE__));

Tested it with your code and it works.

For reference: http://codex.wordpress.org/Determining_Plugin_and_Content_Directories

4
  • @mbadger: this is because your code is setting a relative path (relative to what? when?) whereas Wyck's code is setting an absolute path that will find your classes no matter from where is it called.
    – webaware
    Jan 24, 2013 at 9:35
  • @webaware: Thanks for pointing that out, setting the absolute path like Wyck pointed out did indeed sort the problem. I am curious as to why it worked at all though. My assumption was that it would treat '.' as the server root, which it appeared to do on the front end.
    – mbadger
    Jan 24, 2013 at 9:58
  • Every page / post on the front end effectively runs from the root of the website, as it is routed to {root}/index.php, whereas admin pages generally run from {root}/wp-admin (or from specific plugins' folders).
    – webaware
    Jan 24, 2013 at 10:03
  • Ah, of course! Much appreciated
    – mbadger
    Jan 24, 2013 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.