PHP OOP:每个参数类型的唯一方法? [英] PHP OOP: Unique method per argument type?

查看:125
本文介绍了PHP OOP:每个参数类型的唯一方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个自制的ORM(学术兴趣)。我正在努力遵守TDD概念作为培训课程,作为该练习的一部分,我在开发课程时为API编写文档。



这个例子我正在使用一个经典的getCollection类型的mapper类。我希望它能够检索特定用户的资产X(让我们说博客帖子)的集合,以及基于任意数值数组的集合。所以 - 你可能有一个这样的方法。

  $ User = $ UserMapper-> load(1); 
$ ArticleCollection = $ ArticleMapper-> getCollection(range(10,20));
$ ArticleCollection = $ ArticleMapper-> getCollection($ User);
$ ArticleCollection = $ ArticleMapper-> getCollection($ User-> getId());

所以,在编写getCollection方法的文档时 - 我想在...中声明@param变量文档块。为每个参数类型设置唯一的方法是否更好?或者是否可以根据参数类型将一个方法委托给正确的内部方法/类?

解决方案

可以使用一种方法来委派正确的内部方法。您可以这样记录:

  @param Array | User | Integer $ argName可选说明

但是再一次,没有人阻止你每个都有一种方法

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

此外,您可以使用 magic __ call 方法假装上述方法,然后捕获方法调用并委托给您的内部方法( ZF该fi用于查找依赖数据库行)。您将使用Class DocBlock中的 @method 注释来记录这些方法。但是请记住,使用和/或直接调用相应方法时,魔法方法总是比较慢。



使用您认为最适合您的应用程序和用户界面的内容。


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 documentation for the API as I develop the class.

Case in point - I'm working on a classic "getCollection" type mapper class. I want it to be able to retrieve collections of asset X (let's say blog posts) for a specific user, and also collections based on an arbitrary array of numeric values. So - you might have a method like any one of these

$User = $UserMapper->load(1);
$ArticleCollection = $ArticleMapper->getCollection(range(10,20));
$ArticleCollection = $ArticleMapper->getCollection($User);
$ArticleCollection = $ArticleMapper->getCollection($User->getId());

So, in writing the documentation for the getCollection method - I want to declare the @param variable in the Docblock. Is it better to have a unique method for each argument type, or is it acceptable to have a method that delegates to the correct internal method/class based on argument type?

解决方案

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.

这篇关于PHP OOP:每个参数类型的唯一方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆