Search code examples
phpclasssymfonynamespacesclass-reference

Symfony2 use of class


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?


Solution

  • 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