2

I'm working on a project and I want to try and 'lazy-load' objects.

I've setup a simple class using the Magic Method __call($name, $arguments).

What I'm trying to do is pass the $arguments through, not as an array, but as a list of variables:

public function __call($name, $arguments)
{
    // Include the required file, it should probably include some error
    // checking
    require_once(PLUGIN_PATH . '/helpers/' . $name . '.php');

    // Construct the class name
    $class = '\helpers\\' . $name;    

    $this->$name = call_user_func($class.'::factory', $arguments);

}

However, in the method actually being called by the above, $arguments is being passed through as an array and NOT the single variables, E.G.

public function __construct($one, $two = null)
{
    var_dump($one);
    var_dump($two);
}
static public function factory($one, $two = null)
{
    return new self($one, $two);
}

Returns:

array
  0 => string '1' (length=1)
  1 => string '2' (length=1)

null

Does this make sense, does anyone know how to achieve what I'm trying to?

1 Answer 1

3

Try:

$this->$name = call_user_func_array($class.'::factory', $arguments);

instead of:

$this->$name = call_user_func($class.'::factory', $arguments);
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.