使用返回的对象运行方法
我正在温习我的非框架面向对象 PHP 并决定进行测试。不幸的是,虽然我理解从类调用方法的概念,但这个特定的测试稍微复杂一些,而且我不知道这种特定类型情况的术语是什么。
测试
创建一个 PHP 类,用于解析文件夹中的(未知数量)文本文件,并允许从文件中提取 amount 字段的总值并获取解析文件的文件名。
文件格式:
这些文件是纯文本 csv 文件。假设这些文件包含过去 N 天内更改的付款列表。有 2 种不同类型的行:
- 已收集卡付款 - 类型 = 1、日期、订单 ID、金额
- 拒绝卡付款 - 类型 = 2、日期、订单 ID、原因、金额
示例文件:
1, 20090313,542,11.99
1,20090313,543,9.99
2,20090312,500,some Reason, 2.99
用法示例:
用法可能是这样的:
$parser = new Parser(...);
$files = $parser->getFiles();
foreach ($files as $file) {
$filename = $file->getFileName();
$amount_collected = $file->getTotalAmount(...);
$amount_rejected = $file->getTotalAmount(...);
}
我的问题是:
当类被称为解析器时,如何执行 $file->method() ?我猜您从解析器类中的 getFiles 方法返回一个对象,但是如何使用返回的对象运行方法呢?
我试图用谷歌搜索这个,但由于我不知道这种情况的术语,所以我没有找到任何东西。
非常感谢任何帮助,即使这只是这种情况的术语。
I am brushing up on my non-framework Object Oriented PHP and decided to do a test. Unfortunately, while I understand the concept of calling methods from a class, this particular test is slightly more complicated and I don't know what the terminology is for this particular type of situation is.
Test
Create a PHP class that parses (unknown number of) text files in a folder and allows to extract the total value of amount field from the file and get filenames of parsed files.
File Format:
The files are plain text csv files. Let's assume that the files contain a list of payments changed in the last N days. There are 2 different types of line:
- Card payment collected - type = 1, date, order id, amount
- Card payment rejected - type = 2, date, order id, reason, amount
Example file:
1,20090313,542,11.99
1,20090313,543,9.99
2,20090312,500,some reason, 2.99
Usage Example:
The usage could be something like this:
$parser = new Parser(...);
$files = $parser->getFiles();
foreach ($files as $file) {
$filename = $file->getFileName();
$amount_collected = $file->getTotalAmount(...);
$amount_rejected = $file->getTotalAmount(...);
}
My question is:
How can you do $file->method() when the class is called parser? I'm guessing you return an object from the getFiles method in the parser class, but how can you run methods with the returned object?
I attempted to Google this, but as I don't know the terminology for this situation I didn't find anything.
Any help is much appreciated, even if it's just what the terminology for this situation is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回的对象可以是带有 getFileName 和 getTotalAmount 方法的
ParserFile
类。这种方法非常接近工厂模式,尽管最好将 getFiles 方法设为静态,无需解析器类本身即可调用。尽管我很确定这不是最好的设计。另一种方法是:
因此,您将把文件数组放入
$files
中,但是当您想要解析这些文件时,您将要求$parser
来执行此操作您可以通过将当前的$file
传递给它的方法来实现。我想没有 100% 正确的解决方案,只需使用最适合您的解决方案即可。如果随后遇到问题,请分析、基准测试和重构。
希望有帮助。干杯 :)
PS 希望这不是作业 :D
The returned object could be of class
ParserFile
with getFileName and getTotalAmount methods. This approach would be quite close to the Factory pattern, although it would be a good idea to make the getFiles method static, callable without the parser class itself.Although I'm pretty sure this is not the best design. Another approach would be:
So you'll get the array of files into your
$files
but when you want to parse these files you'll ask$parser
to do it for you by passing the current$file
to its methods.There's no 100% correct solution I guess, just use what's best for you. If you then encounter problems, profile, benchmark and refactor.
Hope that helped. Cheers :)
P.S. Hope this isn't homework :D