Hi there Gian04,
$this->myvar2 will only give you what you want when you access it via the method $mytest->show_var() then that should take the current state of the property your returning from that. Also you have syntax error's in the if clause in the __construct() method.
Also the way as your defining your properties is invalid, when you define them, you need the $ sign, then when you refer to them in the application within a method, you do so like this:-
$this->myvar; or self::myvar; (been a while since I used the latter, so I might be wrong there :))
Have a see at what I have done here:-
<?php
//error reporting
error_reporting(E_ALL|E_STRICT);
//define class
class myclass {
//declare vars: preceding the variable name with var is pre php5, public/private/static isn't //supported by php4
public $myvar1, $myvar2 = "c";//<--listing them is cleaner to read IMO
//create a method for manipulating the vars defined above
function TryThis(){
if ($this->myvar1 == "a") {
return $this->myvar2 = "A";
}
else{
return $this->myvar2 = "B";
}
}
//access those properties from data supplied when the class is instantiated
function show_var() {
//return the outcome
return $this->TryThis();
}
}
//instantiate the class
$mytest = new myclass();
//give a property state
$mytest->myvar1 = "b";
//return the output
echo $mytest->myvar1;// Result a
echo "<br>";
echo $mytest->myvar2;// Result B
echo "<br>";
//call the method
echo $mytest->show_var();
?>
I hope as this makes it a little clearer, __construct() is generally used to import settings during runtime, I use it to import user arrays and set error states if there are files missing that are needed by the application for it to run smoothly.
Importantly, __construct() returns nothing when it is run, and it is only used when the class is first instantiated, but you can give things state, I find this usually to be booleans or true or false, never tried it with strings or int's before.
*If I have misunderstood the usage / meaning of the __construct() function, then please tell me, this is just how I have understood it to be used, and how it works, other than that, it's assumption*
Other than that, I hope that answers your question.
Cheers,
MRb