Object/class comprehension IDEs

19 views
Skip to first unread message

Mike Whiting

unread,
May 22, 2025, 7:58:56 AMMay 22
to redbeanphp
Hi there,

Something I've noticed that RedBeanPHP does very well is comprehension
of bean classes when working with them in PHPStorm. I.e. code
autocomplete etc works fine even though, presumably, the IDE is
working with objects that have been instantiated with dynamic class
names.

Does anyone happen to know any tricks for getting this to work without
using additional tooling like psalm within my IDE?

Cheers!

Dusty Gamble

unread,
May 24, 2025, 4:15:12 AMMay 24
to redbeanphp
I use this base model, and have all of my objects inherit from it.  The "cast" function makes the editor work correctly.

``` php
class My_RedBean_SimpleModel extends \RedBean_SimpleModel
{
    public static function cast($instance): static
    {
        return $instance->box();
    }
    public function type()
    {
        return $this->unbox()->getMeta('type');
    }
    public function __construct()
    {
        # https://stackoverflow.com/questions/41954415/cant-store-boxed-model-in-redbean
        foreach (array_keys(get_object_vars($this)) as $property) {
            if ($property != 'bean')
                unset($this->$property);
        }
    }
    public static function _get_fake()
    {
        $table = model_table(get_called_class());
        assert(!R::findOne($table));
        $row = R::dispense($table);
        foreach (get_class_vars(get_called_class()) as $name => $value) {
            if ($value === null) {
                # red($name . ', ' . $value);
                continue;
            } else if (in_array($name, ['id', 'bean'])) {
                continue;
            }
            $row->$name = $value;
        }
        return $row;
    }
}
const MEASUREMENTS = ['diameter' => '”', 'height' => '”'];
class Category extends My_RedBean_SimpleModel
{
    public $name = "Example";
    ...
}

# example usage
$category = Category.cast($instance)

```

I think that Gabor may have included some part of this into RedBean proper.

The `_get_fake` methods isn't related, but, I use it to create single instances, to set up my tables during tests.
Reply all
Reply to author
Forward
0 new messages