PHP OOP: Unique method per argument type?

前端 未结 3 680
谎友^
谎友^ 2021-01-20 06:44

I\'m writing a little homebrew ORM (academic interest). I\'m trying to adhere to the TDD concept as a training exercise, and as part of that exercise I\'m writing documentat

3条回答
  •  日久生厌
    2021-01-20 07:22

    It is acceptable to have a method that delegates to the correct internal method. You could document it like this:

    @param Array|User|Integer $argName optional explanation
    

    but then again, there is no one hindering you having one method each

    public function getCollectionByRange(array $range)
    public function getCollectionByUser(User $user)
    public function getCollectionByUserId($id)
    

    In addition, you could use the magic __call method to pretend the above methods exist and then capture method calls to them and delegate to your internal methods (ZF does that f.i. for finding dependant database rows). You would document these methods with the @method annotation in the Class DocBlock. But keep in mind that the magic methods are always slower over having and/or calling the appropriate methods directly.

    Use what you think makes most sense for your application and usecase.

提交回复
热议问题