何时在PHP中使用类与函数

编程入门 行业动态 更新时间:2024-10-27 02:20:22
本文介绍了何时在PHP中使用类与函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

灯泡还没有继续下去...

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中使用类与函数

本文发布于:2023-05-29 21:33:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/353310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!