1

Is it possible to call a function and supply it with a class? Something like:

function foo(class $class) {
  // do something with the class
  echo $class->id;
}

// calling the function
foo(class Bar {
  // supplying the class
  public $id, $username, $password, $time, $ip;
});
3
  • 1
    No, you can't supply a class definition. You can supply an instantiated object of that class. Commented Sep 28, 2016 at 11:16
  • @JonStirling Yeah I knew about the instance of a class, but thanks for confirming :-) Commented Sep 28, 2016 at 11:17
  • @Jek your example makes no sense, compared to your question. It looks like you really want the instance of a class. (You do echo $class->id, that would NOT be possible on a class definition because theres no instance?) Commented Sep 28, 2016 at 11:28

1 Answer 1

1

Your example is a little confusing but I think you are looking for an anonymous class:

function foo($class) {
    echo $class->id;
}

foo(new class {
    public $id;
});

Note that anonymous classes are available only for PHP 7+

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.