何时使用 PHP stdClass [英] When to use PHP stdClass

查看:44
本文介绍了何时使用 PHP stdClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码来自 Google 的 recaptcha 库.他们没有使用 stdClass,而是使用了一个单独的类.我理解为什么如果它们是预先设置的属性(为给定值甚至为 NULL),这会很有帮助,但不明白为什么在这种情况下这样做.像他们那样做有什么价值,还是 stdClass 更合适?

The following code comes from Google's recaptcha library. Instead of using stdClass, they used a separate class. I understand why this would be helpful if they were pre-setting properties (either to a given value or even to NULL), but don't see why they did so in this case. Is there any value to doing it as they did, or would stdClass be more appropriate?

class ReCaptchaResponse {
        var $is_valid;
        var $error;
}
$recaptcha_response = new ReCaptchaResponse();
$recaptcha_response->is_valid = false;
$recaptcha_response->error = 'incorrect-captcha-sol';

推荐答案

这部分是见仁见智,但我个人从来没有使用过 stdClass,也从来不明白这样做的意义所以.

This is partly a matter of opinion, but personally I have never used stdClass, and I've never understood the point of doing so.

对象的目的是封装数据和行为,无论是通过向对象添加公共和私有方法、使用继承和多态来构建您的项目,还是仅仅拥有一个定义良好的命名类型.

The purpose of an object is to encapsulate data and behaviour, whether that's by adding public and private methods to the object, using inheritance and polymorphism to structure your project, or just having a well-defined named type.

在某些语言中,例如JavaScript,对象可用作通用键值存储,即使属性是完全临时添加的;在 PHP 中,该角色可以简单而有效地由关联数组填充.

In some languages, e.g. JavaScript, an object is useful as a general-purpose key-value store, even if properties are added completely ad hoc; in PHP, that role can simply and effectively be filled by an associative array.

有些人会使用 stdClass 对象作为键值存储,但这样做他们会失去对数组周围所有函数的使用.正如 ZubaiR 上面指出的,一个对象将作为一种指针(不是与通过引用传递变量相同)而不是根据需要进行复制,因此行为会有所不同,但是如果您不是故意使用 OOP 的封装"方面(在这种情况下,您可能会创建一个类),很难说这是一件好事还是只是一个混乱的机会.

Some people will use stdClass objects as key-value stores, but they lose the use of all the functions around arrays by doing so. As ZubaiR points out above an object will be passed/assigned as a kind of pointer (not the same as passing a variable by reference) rather than being copied as necessary, so will behave differently, but if you're not deliberately using the "encapsulation" aspect of OOP (in which case you would probably create a class), it's hard to say if this is a good thing or just a chance for confusion.

在其他语言(例如 C)中,有一种使用预定义的命名成员定义自定义结构"类型的方法.PHP 没有这样的类型,但是像您展示的那样一个简单的无方法类非常接近.

In other languages, such as C, there is a way of defining custom "struct" types, with pre-defined named members. PHP has no such type, but a simple methodless class like the one you show is pretty close.

在其他语言中不应用于 PHP 的另一个优势是对象的基类可以具有某些功能,甚至 - 如在 JavaScript 或 Ruby 中 - 允许您添加某些功能.在 PHP 中,stdClass 是完全惰性的,无法编辑,正如别处所指出的不是实际上用作其他对象的基类;唯一的魔法"能力是在投射某些内容或 "juggled"object 类型.

Another advantage in other languages that doesn't apply in PHP is that the base class for objects could have some functionality, or even - as in JavaScript or Ruby - allow you to add some functionality. In PHP, stdClass is completely inert and cannot be edited, and as pointed out elsewhere is not in fact used as a base class for other objects; it's only "magic" ability is to be the target class when something is cast or "juggled" to be of type object.

使用命名类的直接好处包括:

The immediate benefits of using a named class include:

  • PHP 引擎以及 IDE 和代码嗅探器等工具知道哪些属性应该"存在,并且可以在您设置无效属性时向您发出警告,例如由于拼写错误
  • 您可以检查传递的特定值是否是正确类型的对象(使用 instanceOf)而不是
  • 稍后您可以轻松地扩展对象以获得额外的功能,或使其成为继承层次结构的一部分,等等

这篇关于何时使用 PHP stdClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆