DEV Community

Discussion on: How I stumbled onto the Reflection API

Collapse
 
cess11 profile image
PNS11

Looks like there's a bug in Category::addChildCategory(), it'll only add objects that are already in the array.

Don't see a constructor so how do you instantiate Category? Got some generic wrapper class that consumes data and target class name and then figures out setter-names?

Collapse
 
andersbjorkland profile image
Anders Björkland

Good catch. It's supposed to be a ! in there.

As for constructor, it is inferred. I configure the properties with some default values (just empty string or empty array). Then I'll set each property with the respective setters like this:

$category = (new Category())
  ->setUuid(UuidGenerator::uuid4() )
  ->setName("Fiction");
Enter fullscreen mode Exit fullscreen mode

Yep. I have a Repository class for communicating with database, and that one will use a generic method that uses reflection to figure out the properties and methods to use for each property.