在Symfony2中调用另一个Bundle的函数

In my app I have two diverse Bundles, BaseBundle and UserBundle. When I'm in one of the controllers of UserBundle, how can I access the functions available in BaseBundle?

I'm in UserBundle and I'm trying to do something like:

$property['x'] = $this->calculateNumber(array($propertyX->indexX, $propertyY->indexY));

This is the error I get:

    Attempted to call method "calculateNumber" on class "Example\UserBundle\Controller\DefaultController".
500 Internal Server Error - UndefinedMethodException

That's where Symfony's namespaces comes in handy. So when you are in UserBundle, just import the class containing the method you want to call:

# UserBundle/Controller/UserController.php
use BaseBundle\Controller\DefaultController;
//...

class UserController extends Controller
{
    /**
     * @Route("/whatever", name="whatever")
     */
    public function whatever()
    {
        $base = new DefaultController(); //instantiate the class containing the desired method
        $property['x'] = $base->calculateNumber(array($propertyX->indexX, $propertyY->indexY)); //call the calculateNumber method
    }
}

You need to extend the BaseBundle with UserBundle and provide a public method called calculateNumber.

BaseBundle example:

<?php

namespace Example\BaseBundle;

class BaseBundle
{
    public function calculateNumber()
    {
        // ...
    }
}

UserBundle example:

<?php

namespace Example\UserBundle;

use Example\BaseBundle;

class UserBundle extends BaseBundle
{
    // ...
}

As you can see, the UserBundle does not contain the requested function.

There are 2 ways of getting it there:

1) create UserBundle\Controller\DefaultController class by extending BaseBundle\Controller\DefaultController class. Then it will contain all parent's functions and properties

2) Create an actual object of BaseBundle\Controller\DefaultController with new() and use it to get the result.

Don't forget to add

use BaseBundle\Controller\DefaultController;

for both cases.