OO PHP - 从类中检索所有属性
是否有一个函数/方法可以从类中检索所有属性?例如下面我的班级的所有属性:
class plantOfTheMonth {
//Declare which centre(s) are being used
private $centre = "";
//Declare the attributes of the current Plant Of The Month
private $name = "";
private $latinName = "";
private $image = "";
private $imageAlt = "";
private $imageLink = "";
private $strapLine = "";
private $description = "";
private $color = "";
private $centres = "";
//Declare variables for error handling
private $issue = "";
private $issueCode = "";
}
Is there a function/method which retrieves all of the attributes from a class? Such as all the attributes from my class below:
class plantOfTheMonth {
//Declare which centre(s) are being used
private $centre = "";
//Declare the attributes of the current Plant Of The Month
private $name = "";
private $latinName = "";
private $image = "";
private $imageAlt = "";
private $imageLink = "";
private $strapLine = "";
private $description = "";
private $color = "";
private $centres = "";
//Declare variables for error handling
private $issue = "";
private $issueCode = "";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
ReflectionClass::getProperties()
来自反射 API 或get_object_vars()
函数。Use
ReflectionClass::getProperties()
from Reflection API orget_object_vars()
function.foreach 也可以完成这项工作,它通过可见的类属性进行迭代 。
A foreach will also do the job, it iterates trough the visible class properties.