Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.