2

I'm trying to have constants everywhere in my code so I don't have to just stick numbers, I know that's bad practice.

So in my root folder I put a class constants, like this:

namespace App;

class Constants{

    public const FREE_TIER = 0;

}

So I include the "use App\Constants;" in my repository class.

Here is the function I try to access my constants class in:

use App\Constants; /* earlier on */ 

/**
 * @param User $user
 * @return array
 */
public function countFreeTiers() : array
{
    $qb = $this->createQueryBuilder('t');
    $qb->select('COUNT(t.tierNumber)')
       ->where('t.tierNumber = :freeTier')
       ->groupBy('t.tierNumber')
       ->orderBy('t.tierNumber', 'ASC')
        ->setParameter('freeTier', Constants::FREE_TIER /* error here */)
       ->setParameter('true', true);

    $query = $qb->getQuery();
    return $query->getResult();
}

This is the error I get:

Attempted to load class "Constants" from namespace "App".
Did you forget a "use" statement for another namespace?

Am I not allowed to just use the "App" namespace?

Brent Heigold
  • 1,213
  • 5
  • 24
  • 50

2 Answers2

1

This is cerad's solution, not mine.

My problem was that my Constants.php was in the root folder of the project.

I had to move the Constants.php file to inside the src folder, so putting it at the root of src (as opposed to the root of the project) fixed it.

Brent Heigold
  • 1,213
  • 5
  • 24
  • 50
0

You can try to put the class file into 'src/Utilities' and symfony will auto load it.

AntmadDG
  • 66
  • 1
  • 6