0

I have a class, let's say "A" and an attribute private $subModules=Array('func1', 'func2'), where func1 and func2 are the names of private methods in class A.

Class A also have a function public, run() where I try to run methods from attribute $subModules:

class A extends B {
  private $subMethods = Array('func1', 'func2');
  private function func1($a) { // do something }
  private function func2($a) { // do something else}
  public function run() {
     foreach ($this->subMethods as $fnc) {
        call_user_func(array($this, $fnc));
    }
 }

Can you tell me what it is wrong with this? I try to do something like this: $this->func1('5');

The error message it is this:

   Argument 1 passed to A::func2() must be an instance of B, none given

Thank you!

0

1 Answer 1

3
call_user_func(array($this, $fcn));

should be:

call_user_func(array($this, $fnc));

That being said... You dont supply parameters to the private methods... Something like the following will work:

<?php

class A {
  private $subMethods = Array('func1', 'func2');
  private function func1($a) { echo '1'.$a; }
  private function func2($a) { echo '2'.$a; }
  public function run($a) {
     foreach ($this->subMethods as $fnc) {
        call_user_func(array($this, $fnc), $a);
    }
 }
}

$a = new A;

$a->run('test');

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, i write example wrong. This is not the problem
Thank you, i didn't realize i wasn't sending the function parameter.
@OsomA please mark it as the answer to your question as well ;)

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.