简体   繁体   English

在PHP中定义未与对象实例一起存储的类变量

[英]Define class variable in PHP that is not stored along with the object instance

I have a simple class: 我有一堂简单的课:

class obj__image extends obj__file {

    public $dimensions;
    public $storedDPI;
    public $colorProfile;

}

When i store an instance of this to my session (for example), the object gets serialized and stored away. 例如,当我将此实例存储到会话中时,该对象将被序列化并存储起来。 That's fine. 没关系。

But is there any way to define a class variable, such as the above $colorProfile so that it will not be stored when storing/serializing the object? 但是,有什么方法可以定义一个类变量,例如上面的$colorProfile以便在存储/序列化对象时不将其存储?

You can control exactly which properties are included in a serialized object by defining a magic __sleep() method for the class. 您可以通过为该类定义魔术__sleep()方法来精确控制序列化对象中包含哪些属性。 The __sleep() method should return an array of property names: those property names listed in the array will be included in the serialized object, any other unlisted properties will be discarded __sleep()方法应返回一个属性名称数组:该数组中列出的那些属性名称将包含在序列化对象中,所有其他未列出的属性将被丢弃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM