简体   繁体   中英

PHP Cannot redeclare class - Require error - Silex Framework

I'm working on a project, but I have a very annoying problem. I use a PHP file rb.php that contains several important classes for the project (File rb.php of the RedBean ORM, all in one). The problem is that I can use the file correctly with a require in a special location, but not in another location.

This is my arborescence:

树状

When I go to index.php, everything goes well, i can do require('rb.php');

<?php

require_once 'vendor/autoload.php';
require('rb.php');
R::setup('mysql:host=localhost;
        dbname=silex','root','');
require('Model_Bandmember.php');

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;



$srcDir = __DIR__;
$app = new Application();
$app['debug'] = true;
$app->register(new DDesrosiers\SilexAnnotations\AnnotationServiceProvider(), array(
    "annot.controllerDir" => $srcDir."\controllers"
));

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => $srcDir.'\views',
));

    $bandmember = R::dispense('bandmember');
    $bandmember->name = 'Fatz Waller';
    $id = R::store($bandmember);
    $bandmember = R::load('bandmember',$id);
    R::trash($bandmember);
    echo $lifeCycle;die();
$app->run();

I have the good value of $lifeCycle. But I would like to use this file in a controller for functions add (), updates () etc .. So I try this :

<?php

namespace App\Controllers;
use DDesrosiers\SilexAnnotations\Annotations as SLX;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
require(__DIR__.'/../rb.php');
/**
 * @SLX\Controller(prefix="article")
 */
class ArticleController
{

    /**
     * @SLX\Route(
     *      @SLX\Request(method="GET", uri="/"),
     *      @SLX\Bind(routeName="articleIndex")
     * )
     */
    public function index(Application $app)
    {
        $articles = R::findAll('article');
        return $app['twig']->render('Article/index.twig', array(
        'articles' => $articles,
        ));
    }
...
...

But i have this error :

Cannot redeclare class RedBeanPHP\RedException in C:\wamp64\www\SilexTest\rb.php on line 6737

Very well, I think that the file must already be present! But if i comment it i have this error :

Class 'App\Controllers\R' not found

This is normal because this class is in the rb.php file that I just commented on.

If I do a require, I have a class redeclare , but if I do not put it, it lacks a class. Any help will be appreciated.

Since the rb is already included so no need to include it anywhere. To use it from the global scope , you've to use \\R :

$articles = \R::findAll('article');

Because, it seems like that, the R is available in the global scope. In this case, you can use use R; at the top of your class, for example:

namespace App\Controllers;

use DDesrosiers\SilexAnnotations\Annotations as SLX;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use R; // <-- Notice this

/**
 * @SLX\Controller(prefix="article")
 */
class ArticleController
{
    // Use: R::findAll('article') in any method in this class
}

You should read about namespace in PHP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM