0

I am working with a folder structure as follows:

classes
    -common.php
    -core.php
modules
    -index/index.php

I am trying to use the common.php in my index.php file and I am facing error:

Fatal error: Class 'classes\Common' not found in D:\xampp\htdocs\devmyproject\modules\index\index.php on line 7

My Code:

commom.php class:

**Directory:/root/classes/common.php**
<?php
namespace classes;

class Common
{

    function __construct()
    {

    }
}

My index.php file which try to use the classes/commom.php

**Directory:/root/modules/index/index.php**
<?php
namespace modules\beneficiary;
use \classes as hp; 
include_once 'config.php';
$common = new \classes\Common();
//To Get Page Labels
$labels = $common->getPageLabels('1');

I am includeing common.php in config.php

$iterator = new DirectoryIterator($classes);
foreach ($iterator as $file) {
    if ($file->isFile())
        include_once 'classes/' . $file;
}

My Try:

It works fine when I use the folder structure as follows:

classes
    -common.php
    -core.php
modules
    -index.php

If I use another folder inside modules it get error? I am not sure about he hierarchy of folders when using namespace can some one help me?

7
  • 2
    Namespaces and folder hierarchies are two entirely unrelated things. How are you including the files the classes are in? Commented Aug 27, 2013 at 7:13
  • I have shown it in my question. Commented Aug 27, 2013 at 7:18
  • I don't see where you include 'common.php'. Commented Aug 27, 2013 at 7:25
  • It is included in include_once 'config.php'; Commented Aug 27, 2013 at 7:26
  • 1
    Then please say so!?!? We can't guess that. Commented Aug 27, 2013 at 7:27

1 Answer 1

2

You have to either include common.php in index.php (better: use one of its relatives include_once or require_once) or set up an autoloader using spl_autoload_register() (or, not recommended, writing an __autoload() function).

Sign up to request clarification or add additional context in comments.

1 Comment

I have properly included also checked it using get_include_files().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.