How do I set a public variable. Is this correct?:
class Testclass
{
public $testvar = "default value";
function dosomething()
{
echo $this->testvar;
}
}
$Testclass = new Testclass();
$Testclass->testvar = "another value";
$Testclass->dosomething();
Inside
class Testclass
:If you are going to follow the examples given (using getter/setter or setting it in the constructor) change it to private since those are ways to control what is set in the variable.
It doesn't make sense to keep the property public with all those things added to the class.
Use Constructors.
For overloading you'd need a subclass:
This code would echo
newVal
.Add getter and setter method to your class.
You're "setting" the value of that variable/attribute. Not overriding or overloading it. Your code is very, very common and normal.
All of these terms ("set", "override", "overload") have specific meanings. Override and Overload are about polymorphism (subclassing).
From http://en.wikipedia.org/wiki/Object-oriented_programming :