Class properties syntax - scope - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Class properties syntax - scope

ITAITA Posts: 463Member
Hi, I'm just wondering about syntax for accessing class properties in PHP5.

I know that I can do this to access a property:

[code]
$this->property
[/code]

I also know that when there are no variables of the same name, you can just use $property (within the class). I also know, that when you want to access a global variable (from inside a function) you have to use the global keyword. I'm a little confused when you do something like this:

[code]
class Test
{
private $property;

function example()
{
$property = 5;
}
}
[/code]

Does the $property in example() refer to the class property or does it refer to a new PHP variable called $property? If it does refer to a new PHP variable, do I need to use $this to access everything all the time or is there a shortcut like, for example:

[code]
class Test
{
private $property;
function example()
{
global $property;
$property = 5;
}
[/code]

Would this refer to the class property now, ie, would $this->property == 5?

Please could someone clarify.

Many thanks.

Isaac

"Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Comments

  • ITAITA Posts: 463Member
    Ok, I fiddled about with some tests, and I've come to the conclusion that you can only use $this->property to access a property. Am I correct?

    Isaac

    "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Sign In or Register to comment.