0

I have a class First and Second

class First{

//methods

}

class Second{

//methods

}

Another class named Third

I need to send those class names dynamically as a parameter to Third class method and load it like below.

class Third{

    function getData($id, $dynamicClass){  // class names first or second as a parameter
        $this->load->model($dynamicClass);
        $id = $this->$dynamicClass->insert($data);
        return $id; 
    }

}

How to achieve this?

9
  • Did you tried to access to property with braces notation $id = $this->{$dynamicClass}->insert($data); ? Commented May 20, 2016 at 13:59
  • So how does $this->$dynamicClass get magically an instance of the class your passing by calling $this->load->model($dynamicClass)? Commented May 20, 2016 at 13:59
  • @dbf : it seems that the OP is using CodeIgniter Commented May 20, 2016 at 14:00
  • @huggilou if that assumption is true and that's something CodeIgniter offers then I would kick the hell out of it .. Commented May 20, 2016 at 14:03
  • Yes using Codeigniter Commented May 20, 2016 at 14:03

1 Answer 1

1

You can access to your new loaded model using this notation :

$this->load->model($dynamicClass);
$id = $this->{$dynamicClass}->insert($data);
Sign up to request clarification or add additional context in comments.

Comments

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.