关于构建 PHP 类的建议
我对 OO PHP 还只是一知半解。我之前制作过一些简单的课程,并使用了许多下载的课程,但我想正确地制作一个课程,并希望能得到一些关于如何做到最好的建议,或者参考哪些资源/教程来正确地理解我的想法缠绕在它周围。
我将从我想要做什么的背景开始。
我正在构建一个程序,该程序接受用户上传的文件(Excel 文件),其中包含列表和各种值。它看起来像这样:
Item, person1, person2, person3
Car, 1, 3, 4
Bike, 5, 0, 1
现在,该类的主要部分用于为每个人创建一个图形,显示他们拥有多少辆自行车和汽车(可能还有更多物品)。因此,我创建了一个带有一些函数的类来指定背景图像、大小等......我认为这很简单。
不过,我想做的是找出将数据传递给该类的最佳方法,以便它可以有效地制作我的图形。我是否应该使用一些 excel 文件读取类来读取 excel 文件中的数据并执行类似的操作:
foreach column {
foreach row {
$data[$vehiclename] = $columnVehicleCount;
}
$image = new MyClassName;
$image->loadData($data);
...
}
然后在我的类中我只是迭代 $data 的键值对?或者我应该在传递数据之前创建一些其他对象形式来存储数据?我的预感是这是正确的做法,也是面向对象编程的要点,但这就是我不知所措的地方。这是我扩展的东西吗?我已经习惯了使用 JSON 的 JS 对象,但仍然不能 100% 确定使用它们的最佳方式。
我会创建某种子类吗?它将如何运作? $thisItem = $image->addItem(自行车); $thisItem->数量(5);
然后在主课中,
foreach($this->items as $item) {
draw($item->name);
resize($item->quantity)
}
我确信最后一点都是错误的,但这就是我在这里的原因。任何帮助或指导将不胜感激。
干杯, 中号
I'm only half-familiar with OO PHP. I have made a few simple classes before, and used many downloaded ones, but would like to make one properly for once and am hoping for some reco's on either how to do it best, or which resource / tutorials to consult to get my head properly wrapped around it.
I will start with the background on how what I want to do.
I'm building a program that takes a user uploaded file (an excel file) which contains a list and various values. It would look something like this:
Item, person1, person2, person3
Car, 1, 3, 4
Bike, 5, 0, 1
Now, the main part of the class is used to create a graphic for each person that shows how many bikes and cars they have (and possibly many more items). So I create the class with some functions to specify background imagery, size etc... that is straightforward enough I think.
What I want to do though is figure out the best way to pass the data to that class so it can efficiently make my graphic. Should I just read through the data in the excel file using some excel file reading class and do something like:
foreach column {
foreach row {
$data[$vehiclename] = $columnVehicleCount;
}
$image = new MyClassName;
$image->loadData($data);
...
}
and then within my class I just just iterate through the $data's key value pairs? Or should I create some other object form to store the data before passing it? My hunch is that is the right thing to do, and sort of the point of OOP but this is where I am at a loss. Is this were I extend something? I am getting used to JS Objects using JSON, but still not 100% certain of the best way to use them.
Would I create some sort of subclass? How would it work?
$thisItem = $image->addItem(Bike);
$thisItem->quantity(5);
And then within the main class something like
foreach($this->items as $item) {
draw($item->name);
resize($item->quantity)
}
I'm sure that last bit is all wrong, but that's why I'm here. Any help or direction would be greatly appreciated.
Cheers,
M
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你根据 oo 原则构建你的课程
考虑每个对象,他能做什么(方法),定义他的内容(属性),
然后考虑如何创建对象(工厂),以及如何使用数据初始化它们(构建器)。
I recommend you to build your classes according to oo principals
think about each object, what can he do (methods) what defines him (properties)
afterwards think about how you create the objects (Factory), and how you initialize them with data(builder).