0

I have a file Orders.php and I want to use class "Test" functions in it. The problem is that the class file is in a project root folder, but I can't use it.

orders.php:

<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Test;

protected function execute(InputInterface $input, OutputInterface $output)
{
    ini_set('memory_limit', '4000M');
    $test = new \Test(true);
    exit;
}

And class file test.php is:

<?php

namespace Test;

class Test
{
     public function __construct($connect = null, $query = null)
     {
        if ($connect)
        {
            $this->connect();
        }

        if ($query)
        {
            $this->query($query);
        }
     }
}

Now the test.php class file is in project root folder. And the orders.php file is in public_html\project\src\AppBundle\Command\order.php

How should I use the class test if it's in the root of the project or any other directory? I write the namespace with the "/" so it should be refering to the root directory, no?

The50
  • 1,096
  • 2
  • 23
  • 47

2 Answers2

2

You will need to autoload test.php if you want to load it from outside an already defined PSR-0/PSR-4 direcotry. You can autoload single files by updating your composer.json file:

{
    "autoload": {
        "files": ["src/test.php"]
    }
}

After updating composer.json, you'll need to run the following command:

$ composer dumpautoload
edcs
  • 3,847
  • 2
  • 33
  • 56
  • My autoload part in composer.json: "autoload": { "files": ["src/test.php"], "psr-4": { "": "src/" }, "classmap": [ "app/AppKernel.php", "app/AppCache.php" ] }, But it class test is still not found after the dumpautoload And the file itself is in src/test.php – The50 Feb 03 '17 at 09:51
0

Found out that I didn't have to use the namespace in my class file. Also autoloader helped. Thank you.

The50
  • 1,096
  • 2
  • 23
  • 47