views:

203

answers:

8

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();
A: 

Overload or override ?? Adding a constructor to your class u can override the default value

Undefined
Okay. I meant override. I edited my above example. Hope it helps.
Cudos
A: 

Inside class Testclass:

pubic function __construct($new_value)
{
    $this->testvar = $new_value;
}
ntd
A: 

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%5Fprogramming :

Polymorphism allows the programmer to treat derived class members just like their parent class' members. More precisely, Polymorphism in object-oriented programming is the ability of objects belonging to different data types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behavior. One method, or an operator such as +, -, or *, can be abstractly applied in many different situations. If a Dog is commanded to speak(), this may elicit a bark(). However, if a Pig is commanded to speak(), this may elicit an oink(). They both inherit speak() from Animal, but their derived class methods override the methods of the parent class; this is Overriding Polymorphism. Overloading Polymorphism is the use of one method signature, or one operator such as "+", to perform several different functions depending on the implementation. The "+" operator, for example, may be used to perform integer addition, float addition, list concatenation, or string concatenation. Any two subclasses of Number, such as Integer and Double, are expected to add together properly in an OOP language. The language must therefore overload the addition operator, "+", to work this way. This helps improve code readability. How this is implemented varies from language to language, but most OOP languages support at least some level of overloading polymorphism.

Scott Saunders
Yeah... Was just using the wrong term
Cudos
Overload and override and set all have specific meanings. Did you get an answer you were looking for about your code? Or are you specifically looking for information about overloading?
Scott Saunders
Just saw your last edit to the question. You're doing it right. As others have said, you can use get and set methods if you wish, but you don't have to. They have advantages, especially if you need to verify that data is of a certain type before setting it.
Scott Saunders
A: 

For overloading you'd need a subclass:

class ChildTestclass extends Testclass {
    public $testvar = "newVal";
}

$obj = new ChildTestclass();
$obj->dosomething();

This code would echo newVal.

Wim
+1  A: 

Add getter and setter method to your class.

pubic function setValue($new_value)
{
    $this->testvar = $new_value;
}

public function getValue()
{
    return $this->testvar;        
}
Undefined
A: 

Use Constructors.

<?php
class TestClass
{
    public $testVar = "default value";
    public function __construct($varValue)
    {
       $this->testVar = $varValue;               
    }
}    
$object = new TestClass('another value');
print $object->testVar;
?>
CodeToGlory
your code makes no sense, it won't output anything
Greg
check now, I changed the testvar to testVar
CodeToGlory
+2  A: 

this is the way, but i would suggest to write a getter and setter for that variable.

class Testclass

{
    public $testvar = "default value";

    public function setTestvar($testvar) { $this->testvar = $testvar; }
    public function getTestvar() { return $this->testvar; }

    function dosomething()
    {
        echo $this->getTestvar();
    }
}

$Testclass = new Testclass();

$Testclass->setTestvar("another value");

$Testclass->dosomething();
marvin
A: 

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.

AntonioCS