gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.3k stars 280 forks source link

A simple way to improve type-hinting #882

Closed ipsod closed 2 years ago

ipsod commented 2 years ago

This requires PHP 8.0 or greater.

class My_RedBean_SimpleModel extends RedBean_SimpleModel
{
    public static function cast($instance): static
    {
        return $instance->box();
    }
}

This lets you do something like:

$product = ModelProduct::cast($product);

which, in my editor, fixes type-hinting more reliably than anything else I tried.

I've also been using it to specify expected variables at the top of my templates (php files included inline with require), much like they are function arguments. It makes the templates fail early and loudly if they aren't passed the proper beans, and it was a simple way to get type hinting to work in those files.

gabordemooij commented 2 years ago

Looks fine, I think we should just provide is as an extra SimpleModel for PHP 8 +.