skaterdav85 Posted February 26, 2010 Share Posted February 26, 2010 I cant seem to get my getMessage() method to echo out a protected property ($_message), but no error is thrown. I know in Java you can do System.out.println(someProperty); so I'm trying to do the same in PHP with echo. Anyone know why this doesn't work? <?phpclass testClass { protected $_message; public function __construct($message) { $this->_message = $message; } public function getMessage() { echo $_message; }}$object1 = new testClass('hello world');$object1->getMessage();?> Link to comment Share on other sites More sharing options...
Synook Posted February 26, 2010 Share Posted February 26, 2010 You wrote it the right way in the constructor! public function getMessage() { echo $this->_message; } Link to comment Share on other sites More sharing options...
skaterdav85 Posted February 26, 2010 Author Share Posted February 26, 2010 i forgot that in PHP you have to reference properties with $this->someProp, unless you make a local variable. In Java it automatically knows which property you're talking about.Btw, I know in Java they use the terms 'class/instance variables' and 'attributes'. In PHP I typically hear the term 'properties'. Do PHP developers also use the terms 'class/instance variables' and 'attributes' synonymously with 'properties'? Link to comment Share on other sites More sharing options...
Synook Posted February 26, 2010 Share Posted February 26, 2010 The correct Java term for an instance variable is actually "field"[1]. The PHP manual uses the term "property"[2], so that is probably a good one to use too. Nevertheless, "instance variable", "property", "field" and "attribute" mean the same thing in any OOP language.Note, however, that the term "class variable" generally refers to static attributes (because such variables belong to the class itself, not to any instance of it).P.S. It is good practice to use the this keyword when refering to instance members in Java anyway - just to avoid later confusion if you create a local variable with the same name (and to make your code more readable). Link to comment Share on other sites More sharing options...
skaterdav85 Posted February 26, 2010 Author Share Posted February 26, 2010 The correct Java term for an instance variable is actually "field"[1]. The PHP manual uses the term "property"[2], so that is probably a good one to use too. Nevertheless, "instance variable", "property", "field" and "attribute" mean the same thing in any OOP language.Note, however, that the term "class variable" generally refers to static attributes (because such variables belong to the class itself, not to any instance of it).P.S. It is good practice to use the this keyword when refering to instance members in Java anyway - just to avoid later confusion if you create a local variable with the same name (and to make your code more readable).Thanks for the clarification Synook! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.