灯泡还没有继续下去...
The lightbulb has yet to go on for this...
我真的很喜欢一个容易理解的解释在php中使用类的优点只是使用函数。
I'd really love an easy to understand explanation of the advantage to using a class in php over just using functions.
这里是我现在想的一个简单的例子,我想知道一个类是否更有效:
Here's a simple example of the thought I'm having now and am wondering if a class is more efficient:
说我有一个我在PHP中构建的迷你日历小部件。我正在考虑调用函数 miniCal('arrayVars1','var2')。但我可能在那个页面上做两次。通过在这里使用一个类使用更少的资源,创建它的新实例?
Say I have a mini calendar widget that I've built in php. I'm thinking about calling the function miniCal('arrayVars1', 'var2'). But I might do it twice on that page. Do use fewer resources by using a class here, create new instances of it?
我应该在这里咆哮的树,因为我觉得现在树是一个砖墙...
What tree should I be barking up here, because I feel like right now the tree is a brick wall...
推荐答案类用于将数据表示为对象。如果您表示类似用户数据或竞价出价,创建User对象或AuctionBid对象可以更轻松地将这些数据保存在一起,在代码中传递,并使读者更容易理解。这些类将具有属性(数字字段,如数字,字符串或其他对象)以及方法(可以对任何类进行操作的函数)。
Classes are used for representing data as objects. If you're representing something like a user's data, or an auction bid, creating a User object or AuctionBid object makes it easier to keep that data together, pass it around in your code, and make it more easily understandable to readers. These classes would have attributes (data fields like numbers, strings, or other objects) as well as methods (functions that you can operate on any class).
t通常在性能方面提供任何好处,但它们很少有任何负面影响。他们的真正好处是使代码更清晰。
Classes don't usually offer any benefits in terms of performance, but they very rarely have any negative effects either. Their real benefit is in making the code clearer.
我建议您阅读 PHP5面向对象编程指南和维基百科OOP条目。
更多推荐
何时在PHP中使用类与函数
发布评论