Jump to content

read only property


boen_robot

Recommended Posts

Is there any way a property (variable) that is part of a class, defined by myself could be made read only, so that it singlas an error if someone tries to override it or something?

Link to comment
Share on other sites

uhm... like, you have a class definition and there are functions in the class and you want for a function to never be overwritten, even if a user uses classname extends yourclass? Uhm.. I'm not exactly sure, but i believe you could call the function as a private functiona and that would do it.

<?phpclass ClassName {  function ClassName($something=false){   //etc   }  private function helloWorld(){ 	 return "Meow?";  }  //Etc etc}?>

Is that what you're talking about?

Link to comment
Share on other sites

Um. Nope. Not even close to it.First stop, I'm talking about a property. That is, a variable in a class definition. Like so:

<?phpclass ClassName {  $myVar = 'hello world';}?>

which is then callable in a similar fashion to methods, but without any brackets for arguments like so:

$x = new ClassName;echo $x->myVar;

The above will output "hello world" assuming the class above was used.The problem is this variable may as well be set by the user and given a value, like so:

$x = new ClassName;$x->myVar = 'user value';echo $x->myVar;

This will output "user value". How could I make it so that trying to set a custom value would either result in an error and/or the internal value being used? Actually, how do read only properties in compiled classes work? I've never tryed adjusting a read only value. In any case, I'd like to mimic that sort of behaviour.

Link to comment
Share on other sites

OHHH i think i get what you're saying now. Uhm i'm not exactly sure, but you may want to see if this works:

class Meow{ private var $something;}

That SHOULD only give access to $something inside the class itself... SHOULD being the keyword, i'm not sure how close PHP and C++ are on class inheritance and privacy.either that or instead of var try static. But that i believe only lets you set it once.

Link to comment
Share on other sites

The property needs to be readable outside the class definition (just not adjustable). Using "private" makes the call

$x = new ClassName;echo $x->myVar;

invalid, and that part needs to be valid.Using static only means there's no instance needed (right?). So this will make the following call valid:

echo ClassName::myVar;

and I don't need to make that valid. Infact, when I think about it, it shouldn't be allowed for my case.

Link to comment
Share on other sites

yeah apparently there is no other keyword other than protected, private, and public. Public means that you can edit it, protected and private can't be echoed out directly. You would have to do something like this:

<?phpclass ClassName{   protected $thingy   ClassName($item){		 $this->thingy = $item;   }   public function whatIsThingy(){	   return $this->thingy;   }}$test = new ClassName('Hello world');$thingy = $test->whatIsThingy();// etc etc?>

My solution to something like this would be:

<?phpclass className{	 protected $meow,$oink;	 	 function className($something,$somethign2){		 $this->meow = $something;		 $this->oink = $somethign2;	 }	 	 function whatIs($item){		 foreach($this as $key=>$var){			 if($key == $item){				 $this->$key;				 return $this->$key;			 }		 }		 return false;		 	 } }?>

This works in php5, i dont know about 4. basically you would do something like this:

include('./path/to/class.php');$class = new ClassName("Hello","World");$string = $class->whatIs('meow').$class->whatIs('oink');

Link to comment
Share on other sites

I don't need to be compatable with PHP4. I mean, it's not a must.So, what you're practically suggesting is that I make a private/protected variable and allow it to be read with a method? Well, that may be problematic.I need to use a property. The class that I'm referring to (in case you haven't figured that out already) is XML_XSLT2Processor, from my signature. The class is supposed to be backdraw compatable with PHP's XSLTProcessor class. Adding a new method means code written with XML_XSLT2Processor, reverted to XSLTProcessor will result in an error, due to the lack of my custom method, and I'd like to avoid that. Using a property doesn't result in this behaviour (I tested it). It results in an empty string when echo-ed (maybe FALSE if in a condition... I'm not sure, I didn't made full tests).Oh well, it doesn't matter that much, as if you pass a value, it's not used within the class anyway. The only annoyance is you won't be able to get the real value back.

Link to comment
Share on other sites

About the only thing that doesn't allow you to change it is a constant, but a constant has to be a literal value. The public, private, and protected keywords only control visibility of the property.
Exactly. And I need to adjust that property within the class, then allowing the user to read it. So a constant is not a solution either.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...