开发者

Recommendation on building a PHP Class

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 col开发者_如何学编程umn {
    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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜