Jump to content

Echo A Protected Property Inside A Public Method


skaterdav85
 Share

Recommended Posts

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

:) 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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...