将属性添加到PHP中的对象

编程入门 行业动态 更新时间:2024-10-27 19:21:01
本文介绍了将属性添加到PHP中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在PHP中向对象添加属性?

How do you add an attribute to an Object in PHP?

推荐答案

好吧,向对象添加任意属性的一般方法是:

Well, the general way to add arbitrary properties to an object is:

$object->attributename = value;

您可以在类中更清晰地预定义属性(特定于PHP 5+,在PHP 4中,您将使用旧的var $attributename)

You can, much cleaner, pre-define attributes in your class (PHP 5+ specific, in PHP 4 you would use the old var $attributename)

class baseclass { public $attributename; // can be set from outside private $attributename; // can be set only from within this specific class protected $attributename; // can be set only from within this class and // inherited classes

强烈建议这样做,因为您还可以在类定义中记录属性.

this is highly recommended, because you can also document the properties in your class definition.

您还可以定义 getter和setter方法您尝试修改对象的属性.

You can also define getter and setter methods that get called whenever you try to modify an object's property.

更多推荐

将属性添加到PHP中的对象

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

发布评论

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

>www.elefans.com

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