dongsu3654 2014-01-25 17:16
浏览 19
已采纳

何时使用PHP 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';
  • 写回答

4条回答 默认 最新

  • dongzhuanlei0768 2014-01-25 17:45
    关注

    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.

    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.

    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.

    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.

    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:

    • the PHP engine, and tools such as IDEs and code sniffers, know which properties "should" exist, and can warn you if you set invalid ones, e.g. through a spelling mistake
    • you can check if a particular value passed is the right type of object (using instanceOf) rather than
    • you can easily expand the object later to have extra functionality, or make it part of an inheritance hierarchy, etc
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分