Default values in php class doesn't work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyman1234
Forum Newbie
Posts: 1
Joined: Tue Jul 07, 2009 3:02 pm

Default values in php class doesn't work

Post by dannyman1234 »

Hi I have a class here:

class plattegrond extends mysql{

public $var1 = '5';
public $var2 = '5';
public $sum;


public function get_test() {
$this->sum = $this->var1.''.$this->var2 ;
return $this->sum;
}

What I get here is 00. Becouse ther var1 en var2 are empty it gives me 0 as a result. Why not use the default values 5 ?

Hope someone can help
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: Default values in php class doesn't work

Post by Skara »

uhh.. the code you have above would return '55'
If you're getting '00', then your problem is elsewhere.

If you're trying to add the two values together (I gather this since you're using the word sum as the function name, then the code would need to be..

Code: Select all

public $var1 = 5; //no quotes, it's an integer
//...
$this->sum = $this->var1 + $this->var2;
Post Reply