0

Most of my classes in bundle are having same name. CRMFirstSecondExternal or CRMSecondThirdExternal and so on. Now I need to store the FirstSecond or SecondThird to a $variable and then use that $variable in between the class name to make it a proper call to that class.

use Escalon\Bundle\Admin\CRMBundle\Helper\CRMTravelExpenseExternal;
private function deletePreviousSchedule( $params )
{
    $queryParams = array();
    $em = $this->getDoctrine()->getManager();

    if($params['bundleAndTableName'] == 'EscalonAdminCRMBundle:ClientServiceTe')
    {
        $helper = 'TravelExpense';
    }
    //$helper = preg_replace("/[^a-zA-Z]/", "", $helper);
    $crmEntityObject = new CRM.$helper.External;
    $crmEntityObject -> deletePreviousScheduleExternal($params);
}

Error: Fatal error: Uncaught Error: Class 'Escalon\Bundle\Admin\CRMBundle\Controller\CRM' not found in.

1 Answer 1

1

The problem is that class name is not concatenated correctly. Try this function

private function deletePreviousSchedule($params)
{
    $queryParams = array();
    $em = $this->getDoctrine()->getManager();

    if($params['bundleAndTableName'] == 'EscalonAdminCRMBundle:ClientServiceTe')
    {
        $helper = 'Escalon\Bundle\Admin\CRMBundle\Helper\CRMTravelExpenseExternal';
    }

    $crmEntityObject = new $helper();
    $crmEntityObject->deletePreviousScheduleExternal($params);
}

You can find more documentation on this here - http://php.net/manual/en/language.namespaces.dynamic.php

Also, it's possible to do

use Escalon\Bundle\Admin\CRMBundle\Helper\CRMTravelExpenseExternal;
private function deletePreviousSchedule($params)
{
    $queryParams = array();
    $em = $this->getDoctrine()->getManager();

    if($params['bundleAndTableName'] == 'EscalonAdminCRMBundle:ClientServiceTe')
    {
        $helper = 'TravelExpense';
    }

    $className = "Escalon\Bundle\Admin\CRMBundle\Helper\CRM{$helper}External";
    $crmEntityObject = new $className();
    $crmEntityObject->deletePreviousScheduleExternal($params);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi friend thanks for the solution. Its working. But in my case I need to access other class as well depending on if condition. So I need a solution throw which I can concatenate the variable in between the class name as i has mentioned in my question.
I'm not sure why you can't use the first solution to access different classes depending on if conditions, but I added another solution. Also, you don't need the use Escalon\Bundle\Admin\CRMBundle\Helper\CRMTravelExpenseExternal; on the first line when you specify class with namespace.
I appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.