Badger
Badger

Reputation: 487

Trying to call an array of arguments on a function

I have an array of arguments to pass to a method where the number of arguments is variable. I have absolutely no idea how to do it.

class Entity {
    protected $realObject;

    public function __call($name, $arguments) {
        // just call this method on the $property, otherwise redefine the method
        // error_log("Called $name");

        // TODO support variable numbers of arguments
        $argc = count($arguments);
        if($argc > 0) {
            return $this->realObject->$name($arguments[0]);
        } else {
            return $this->realObject->$name();
        }
    }

}

I've been looking at all sorts of ways of doing it, but can't seem to work out a way of turning an array into a variable of variables.

Upvotes: 0

Views: 47

Answers (3)

yivi
yivi

Reputation: 47369

There is built-in support for this in PHP >= 5.6 (which you should be running at the very least).

E.g.:

$parameters = ['parameter1', 'parameter2', 'parameter3'];

function iAcceptManyParameters(...$parameters) {
   foreach ($parameters as $parameter) {
      echo $parameter, "\n";
   }
}

iAcceptManyParameters(...$parameters);

You can see it working here.

Upvotes: 1

ffflabs
ffflabs

Reputation: 17481

Ok, you see, magic method __call of a class is called when you try to execute a method of said class you don't have access to. So, in this case:

class Entity {

    protected function realObject($a, $b, $c) {

    }

    public function __call($name, $arguments) {

        $argc = count($arguments);

        if($argc > 0) {
            return $this->$name($arguments[0],$arguments[1],$arguments[2]);
        } else {
            return $this->$name();
        }
    }

}

Now, let's say you instance this class and try to call a private method

$myEntity = new Entity();

$myEntity->realObject('hello','my','friends');

The protected method invocation is intercepted by __call, so it's like you had called

$myEntity->__call('realObject',['hello','my','friends']);

Whatever you decide to do inside your __call method, that is up to you. In my example, it just forwards your call to the protected method.

Upvotes: 0

Gravy
Gravy

Reputation: 12445

Is this what you want to do?

class Entity
{
    protected $myClass;

    public function __construct(string $myClass)
    {
        $this->myClass = new $myClass;
    }

    public function __call($name, $arguments)
    {
        return $this->myClass->$name($arguments);
    }
}

class TestClass
{
    public function foo($msg)
    {
        return $msg;
    }
}

$entity = new Entity('TestClass');
var_dump($entity->foo('bar', 'baz'));
die();

output:

array(2) {
  [0]=>
  string(3) "bar"
  [1]=>
  string(3) "baz"
}

But based on your comment: Yes, it's an abstraction of somebody else's classes. My object is a copy of their object, why don't you just create your class, which extends their class? e.g class MyEntity extends TheirEntity {...}. Your class will then inherit their public and protected properties & methods.

Upvotes: 0

Related Questions