1

Hope this is just something quick, but I can't find something that works.

I want to check if a function exists in a model from a controller in CodeIgniter, and if it does, it should run the function.

I have tried the following:

$comp_model = 'model_comp_'.$comp_code;
$this->load->model('comp/'.$comp_model);

if (function_exists($this->$comp_model->join_comp))
...

and

if (method_exists($this, $comp_model->join_comp()))
...

even just calling the function in the if statement

if ($this->$comp_model->join_comp())
...

Please help!

6
  • No use of Your Question. This no longer proceed any answer Commented Aug 6, 2015 at 8:01
  • I am not sure what you mean? Commented Aug 6, 2015 at 8:09
  • whats is the purpose of checking that?? Commented Aug 6, 2015 at 8:09
  • rtfm Commented Aug 6, 2015 at 8:10
  • I need to automatically write something in a database, but only if that function exists...To Explain, we are a dance competition organization, and for some of our competitions you need to apply for memberships and others you are automatically approve, and if that function exists, you are automatically approved Commented Aug 6, 2015 at 9:08

2 Answers 2

1

I had the same problem and solved it this way:

$modelName = 'some_model';
$pageModelPath = APPPATH . 'models' . DIRECTORY_SEPARATOR . $modelName . '.php';
if( file_exists( $pageModelPath ) ){
    require $pageModelPath;
    $model = new $modelName;

    if( method_exists($model, 'myFunction') ){
        $model->myFunction();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Can't you just implement the method in all models? I think you should create a parent with an protected method called join_comp() and make all your models derive from that parent. You could just add the 'automatic approval' code to the specified model.

1 Comment

I scratched the idea, decided its easier to make it so that when the school does the first entry, it will be added to the database, and when the last entry is removed, the school is removed as well, just makes a bit more sense to me, but thanx for the 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.