How to mass create getters

I am building an adapter class that we will use to wrap around an ActiveRecord to convert it to/from another class (used by our API). Our app has to store a lot of different types of entities and I’d like to make this as scalable as possible. We’ll end up using this base class in many child classes over time, so working very hard to minimize any need to hand-write code in the child classes.

Naturally, my thinking was to make this data-driven and build out a table like so in my class:

$attrMap => [
	'publicAttribute1' => [
		'dbFieldName' => 'my_db_fk_id1',
		'type' => 'reference',
	'publicAttribute2' => [
		'dbFieldName' => 'my_db_field_str2',
		'type' => 'string',

This works great in rules() and fields() in that I can run a foreach() loop. For fields(), this even includes the logic to call a private getValue() that parses out what to do (some fields are just literals, while others reference other tables, etc). Works great!

Except, we still have to manually define our getters/setters.

Is there a way to include that in this data-driven process? That is, is there a way to have, say, a getter that works like this:

public function myGetter($attrName)
	return $this->getValue($attrName);

I have clearly read the Yii docs to NOT mess around with PHP getters/setters directly, so I am not doing that. But I’m wondering if Yii has a way for me to do this? It works fine in rules() and fields(), but not clear how to solve this for get()/set().

Magic __get is your friend here.

Yes, but the Yii2 docs specifically say to not do that, which is why I was looking for another Yii2 “supported” option. :frowning: