Jek Jek - 2 months ago 7
PHP Question

Give PHP function class object

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;
});

Answer

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+

Comments