[英]php hide attributes on casting to array
I coded a php class to represent a query-result. 我编写了一个php类来表示查询结果。 On an incoming query I cast it to an array, convert it to JSON (per json_encode) and return it to the user.
在传入的查询中,我将其转换为数组,将其转换为JSON(每个json_encode)并将其返回给用户。
Now I want to define a class intern "debug" attribute, which souldn't be in the output - how can I hide attributes on casting a class to an array? 现在我想定义一个类实习生“debug”属性,它不应该在输出中 - 如何在将类转换为数组时隐藏属性?
使用unset
从结果数组中删除元素。
Declare it private
or protected
. 将其声明为
private
或protected
。
class Foo
{
public $bar = 'bar';
private $baz = 'baz';
protected $quux = 'quux';
}
$f = new Foo();
echo json_encode($f);
Result: 结果:
{"bar":"bar"}
NB. NB。 Requires PHP 5, see Visibility in the PHP manual.
需要PHP 5,请参阅PHP手册中的Visibility 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.