XF 2.0 Extending Account and Member controller - when to use "use namspace"

abdfahim

Well-known member
While extending member controller, I need to add "use XF\Mvc\ParameterBag" line, otherwise it throws an error
PHP:
use XF\Mvc\ParameterBag;

class Member extends XFCP_Member
{
    public function actionNewTab(ParameterBag $params)
    {
        $user = $this->assertViewableUser($params->user_id);
        $viewParams = [
            'user' => $user
        ];
        return $this->view('XF:Member\Timeline', 'abdfahim_newtab', $viewParams);
    }
}

However, while extending Account, the following code works without adding "use XF\Mvc\FormAction", though I use FormAction inside a function
PHP:
class Account extends XFCP_Account
{

    protected function newTestSaveProcess(\XF\Entity\User $visitor)
    {
        $form = $this->formAction();

        ...................
        ...................

        return $form;


    }
}

Anyone please explain?
 
Use statements are just a way of defining a shortcut for a class name used within the file. It relates specifically to how the class names are written, not whether an object is used which happens to be an instance of that class.

So in your example use XF\Mvc\ParameterBag; allows you to use only ParameterBag here actionNewTab(ParameterBag $params) rather than having to type actionNewTab(\XF\Mvc\ParameterBag $params).

You don't actually directly write the FormAction class name in the Account.php file so the use declaration isn't needed.
 

Similar threads

Top Bottom