require_once at symfony

后端 未结 4 1499
一向
一向 2021-01-07 10:46

I\'m making now things with php + Symfony2 framework, and I have the following code:

require_once(\"one_file.php\");
require_once(\"another_file.php\");


        
相关标签:
4条回答
  • 2021-01-07 11:24

    You can follow the PSR-0 standard to let the autoloader handle this. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md and http://getcomposer.org/doc/04-schema.md#psr-0 .

    Or you could keep your files as is, and tell composer to require them each time : http://getcomposer.org/doc/04-schema.md#files

    0 讨论(0)
  • 2021-01-07 11:26

    You want to follow these other answers, especially the approved one, but if you are using a third party library with tons of PHP files, you can do require_once(__DIR__.'/path/to/file.php') as a quick fix.

    0 讨论(0)
  • 2021-01-07 11:37

    You have to make a folder in your acme folder like lib puts these files in lib folder then use this statement

    use Acme\DemoBundle\lib\Abc; // its your class name 
    
    0 讨论(0)
  • 2021-01-07 11:50

    If these classes reside inside bundle then you could use as below: Suppose your bundle name is AcmeDemoBundle. Place this file inside Acme/DemoBundle/Model/

      //one_file.php
       namespace Acme/DemoBundle/Model;
       class one_file {
       ...........
       }
    

    To use this file inside your controller or any other file:

    Here for Acme/DemoBundle/Controller/UserController.php

       namespace Acme/DemoBundle/Controller   
       use Acme/DemoBundle/Model/one_file
        class UserController {
        public $one_file=new one_file();
        }
    

    In php 5.3 onwards, namespaces has been introduced. You should probably look at namespaces and its uses in php documentation

    0 讨论(0)
提交回复
热议问题