I don't think this is the best way to be explaining method chaining to someone unfamiliar. Creating a factory for classes just to add method chaining isn't really best practice. Consider if a class' methods all return a specific value. Method chaining would be useless. If you want to take advantage of method chaining, modify the classes as you see fit, using return $this;
where appropriate. There are lots of great examples out there, like Eloquent as mentioned, amongst others (most ORMs are a good example, really).
You also have a weird coding style that's difficult to follow. Multiple lines for method parameters and ternary ops, non-descriptive variables, etc. That's fine for your own applications, but if you're trying to teach people, keep it as simple as possible.
You don't need two nth-child
selectors, just give the default style to the original tr
element. If you want to do every other row starting with the second, 2n
or even
is sufficient.
I do this every time I want to create a stdClass out of an array. Except when the array is empty by default, then there's no real time saver over new stdClass;
I wouldn't suggest someone use a wrapper for the sole purpose of adding method chaining in the first place, so I don't have a better solution.
And regardless of the length or depth of this tip, you're still attempting to teach people something. Your code style makes that more difficult. Just something you may want to consider in the future.