Object oriented php

Hi guys,

I have been using mainly procedural php over the past months but now I am writing oo scripts and I have a question about public and private attributes.

I once believed that public attributes declared at the top of a class like:
public $var = 2;

were accessible for manipulation by all member functions without the object reference but now I find that in my php programs, I can only give them visibility to one another by using $this->var.

Why can’t a method doit() access and manipulate a variable using $var? Why do I have to keep using the object reference?

thanks

You always have to use $this-> or $object-> when accessing the properties/methods of an object in PHP, it has always been this way.

OK that’s great, thanks for the confirmation. I have programmed in C++ before and you don’t have to do this which is why I was confused. Thanks again :slight_smile: