How to hide away properties of an object?

40 views (last 30 days)
I have a class in Matlab, which has a number of properties. I want to make a pcode to obfuscate content of the class. But I couldn't find any way to hide properties of the class: using struct(obj) all the properties become available. Is there any workaround?
  1 Comment
Sean de Wolski
Sean de Wolski on 6 Oct 2017
In addition to struct() you can also call metaclass (or ?classname) on it and get everything that way...

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 6 Oct 2017
Edited: Guillaume on 6 Oct 2017
I don't think there's any way to hide private properties from struct.
The only workaround I could think of would be to store the private properties in another class (handle) and use a persistent containers.Map in a function to access that class. Would be a bit of a hassle, so you need to really want to hide these properties:
classdef ClassWithPropertiesToHide
properties (Access = public) %properties that are public, hence don't need hiding
something;
end
properties (Access = private)
objectID; %will be visible through struct, but useless. Used as key in the container.Maps which unfortunately can't use a class as a key
end
methods (Access = public)
function this = ClassWithPropertiesToHide()
persistent idcount; %unique id for each class instance to be used as key in the map
if isempty(idcount)
idcount = 0;
else
idcount = idcount + 1;
end
this.objectID = idcount;
privateprops = GetPrivateProperties(this); %privateprops is a handle class, so can be modified here
privateprops.someprop = somevalue;
privateprops.someotherprop = someothervalue;
end
function someotherfunction(this)
privateprops = GetPrivateProperties(this); %get private properties
%do something
end
end
methods (Access = private)
function privateprops = GetPrivateProperties(this)
persistent propcontainer;
if isempty(propcontainer)
propcontainer = containers.Map;
end
if ~isKey(propcontainer, this.objectID)
propcontainer(this.objectID) = PrivatePropertiesClass %PrivatePropertiesClass is the class to hold the private properties. Its properties have to be public
end
privateprops = propcontainer(this.objectID);
end
end
end
Edit: The main class probably needs a destructor and a slightly more sophisticated implementation of GetPrivateProperties so that when an instance of the main class is destroyed its private properties are also removed from the map. Otherwise, memory usage can only go up. Left as an exercice to the reader...
  5 Comments
Hodjat Rahmati
Hodjat Rahmati on 6 Oct 2017
Yeah you're right; the properties are hidden, however modifying the properties is gonna be a painful job because firstly the properties are called, they they should be passed to some other functions that modify the parameters and theses changes has to be applied on the PrivatePropertiesClass somehow!
Guillaume
Guillaume on 6 Oct 2017
As I tried to show in the code of someotherfunction, it's actually not that difficult to operate on the private properties, you just obtain a reference to the private properties object at the beginning of the function and use the (public) properties of that object as if they were the private properties of your class. Because that private property class is a handle class, you don't have to apply these changes:
function someotherfunction(this)
privateprops = GetPrivateProperties(this);
privateprops.SomeProp = privateprops.SomeProp + 1;
%someprop is permanently update for the current instance. No need to apply back
end
But yes, as I said a bit of a hassle.
A bigger issue is that you also need to overload saveobj and loadobj to actually save the private properties now that they're not actually properties, otherwise they will be lost through save and load.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!