1

I have a class called DataBaseHelper which is defined in the file DataBaseHelper.php:

<?php

namespace Company\Project\System\DataBaseHelper;

use Company\Project\System\RepoLink;
use mysqli;
use mysqli_result;

class DataBaseHelper
{
    /* some cool stuff */
}

Inside of a file called helper.php (with the namespace Company\Project\System;) I use the following use Statement: use Company\Project\System\DataBaseHelper\DataBaseHelper;:

 <?php

namespace Company\Project\System;

use Company\Project\DataBaseHelper\DataBaseHelper\DataBaseHelper;

class CustomHelper
{
    /* some cool stuff */

    public function getDownloadCount(): string
    {
        try {
            $query = "SELECT sum(count) c from downloads";

            // The next line is line 258
            $DBLink = new DataBaseHelper();

            $result = $DBLink->getAssocResult($DBLink->executeQuery($query));

            return $result["c"];
        } catch (\Exception $e) {
            // TODO: Errorhandling
            return "ERROR";
        }

    }

}

Calling CustomHelper->getDownloadCount() throws the following error:

> [15-Sep-2019 07:48:09 Europe/Berlin] PHP Fatal error:  Uncaught Error: Class 'Company\Project\System\DataBaseHelper\DataBaseHelper'
> not found in
> /Applications/XAMPP/xamppfiles/htdocs/project/system/helper.php:258
> Stack trace:
>     #0 /Applications/XAMPP/xamppfiles/htdocs/project/manage/index.php(49):
> Company\Project\System\RepoHelper->getDownloadCount()
>     #1 {main}   thrown in /Applications/XAMPP/xamppfiles/htdocs/project/system/helper.php on
> line 258

Is there anything missing with the namespaces? The use Statement inside helper.php was automatically generated by my IDE (PHPStorm).

I do not use any frameworks.

Fabian
  • 541
  • 9
  • 30
  • Have you used any framework? – sh1hab Sep 15 '19 at 06:27
  • @shihab No I do not use any framework (i will add this information to my question) – Fabian Sep 15 '19 at 06:29
  • 1
    in helper.php file you have used `use Company\Project\DataBaseHelper\DataBaseHelper;` shouldn't it be `use Company\Project\System\DataBaseHelper\DataBaseHelper;` – sh1hab Sep 15 '19 at 06:37
  • @shihab yes you are right - it was just a silly copy paste error here. I use it with `Company\Project\System\DataBaseHelper\DataBaseHelper` (Thats what the IDE automatically imported). – Fabian Sep 15 '19 at 06:40
  • How/where are you loading the file with the class `DataBaseHelper.php`? Are you using any kind of autoloader? – yivi Sep 15 '19 at 14:31
  • How did you run your `helper.php` file? How did you make sure `DataBaseHelper.php` is loaded before you're calling the class `DataBaseHelper` in it? – Koala Yeung Sep 16 '19 at 05:07
  • @KoalaYeung the helper.php file is included in my index.php via the use statement: `use Company\Project\System\CustomHelper;`. How do I make sure `DataBaseHelper.php` is loaded? I think it should be loaded via the use statement or am I wrong? – Fabian Oct 04 '19 at 20:36
  • @Fabian: The `use` statement loads no file unless you otherwise programmed it to. See my answer. – Koala Yeung Oct 05 '19 at 01:01

1 Answers1

0

A class is not available to a PHP file unless the class is:

  1. Declared in that file; or
  2. Declared in another PHP file that is inculde or require by that file directly or indirectly.

The use statement only declared alias for long namespaced class to have shorter local names. It does not automatically include any file. In fact, there is no default definition on how a namespaced class should be placed in a folder structure at all.

To automatically include or require a class file on new, you need to implement a register autoloader function with spl_autoload_register or have composer implement it for you.

You may reference this answer to a similar question for more details.

Your PhpStorm is probably suggesting the namespace assuming you'd use composer in a way that follows the PSR-4 standard.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50