[英]how can i access properties defined dynamically inside php class
trying to develop a class associated with table(like in frameworks). 尝试开发与表相关的类(例如在框架中)。 assume we have a class named Book
假设我们有一个名为Book的类
class Book
{
public function save()
{
....
}
}
$book = new book;
$book->id = '1';
$book->name = 'some';
$book->save();
the problem is how can i access this dynamically created properties inside save() to save new record 问题是我如何才能在save()中访问此动态创建的属性以保存新记录
You could do it that way (note that there are other solutions to this problem) : 您可以采用这种方式(请注意,此问题还有其他解决方案):
public function save() {
$properties = get_object_vars($this);
print_r($properties);
// do something with it.
}
You can find properties in an object with: 您可以使用以下方法在对象中找到属性:
$properties = get_object_vars($book);
See: http://php.net/manual/en/function.get-object-vars.php 请参阅: http : //php.net/manual/en/function.get-object-vars.php
here is complete code that you should use: 这是您应该使用的完整代码:
<?php class Book { public function save() { $vars = get_object_vars($this); var_dump($vars); } } $book = new book; $book->id = '1'; $book->name = 'some'; $book->save(); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.