Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8311683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:53:54+00:00 2026-06-08T19:53:54+00:00

I have been fiddling around with Namespace in PHP and was trying to make

  • 0

I have been fiddling around with Namespace in PHP and was trying to make it work, but it fails

Let me show the example code:

test\views\classes\MainController.php

<?php
namespace test\views\classes;

class MainController
{
    public function echoData()
    {
        echo 'ECHOD';
    }
}

test\views\index.php

<?php
require_once '..\autoloader\autoloader.php';

use test\views\classes\MainController;

$cont = new MainController();

$cont->echoData();

test\autoloader\autoloader.php

<?php

spl_autoload_register(null, FALSE);

spl_autoload_extensions('.php');


function classLoader($class)
{
    $fileName = strtolower($class) . '.php';

    $file = 'classes/' . $fileName;

    if(!file_exists($file))
    {
        return FALSE;
    }

    include $file;
}

spl_autoload_register('classLoader');

Throws an error:

Fatal error: Class 'test\views\classes\MainController' not found in ..\test\views\index.php on line 6

Am Im missing something!

EDIT: The code works fine when both the index.php and maincontroller.php are in the same directory without using autoloader but using require_once(‘maincontroller.php’);
Does not work if they are in different directories and with autoloader function. Can anyone sort this out.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:53:55+00:00Added an answer on June 8, 2026 at 7:53 pm

    Multiple problems in your code:

    The namespace separator (\) is not a valid path separator in Linux/Unix. Your autoloader should do something like this:

    $classPath = str_replace('\\', '/', strtolower($class)) . '.php';
    if (!@include_once($classPath)) {
     throw new Exception('Unable to find class ' .$class);
    }
    

    Plus, the paths are all relative. You should set your include path. If your site structure is like this:

    bootstrap.php
    lib/
      test/
        views/
          index.php
          classes/
            maincontroller.php
      autoloader/
        autoloader.php
    

    Your bootstrap.php should look similar to:

    $root = dirname(__FILE__);
    $paths = array(
        ".",
        $root."/lib",
        get_include_path()
    );
    set_include_path(implode(PATH_SEPARATOR, $paths));
    include 'lib/test/autoloader/autoloader.php';
    

    Now, in your test/views/index.php you can just include the bootstrap:

    include '../../bootstrap.php';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been fiddling around with server side interceptors on CXF. But is seems
I have this, and i've been fiddling around with it for a while but
I have been fiddling with code to call a function with the name of
Have have been trying to make a validator for my xml files. I have
I have been fiddling around with settings for a while, and cannot seem to
I have been using this rule: RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L] Which will turn example.com/page/hello
Have been playing around with linq but there is one thing I cant seem
I have been fiddling with JMS Virtual topics. I want to get the example
I've been fiddling around with this for quite some time. I can't work out
I have been fiddling with a few lines of code for two days now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.