homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Need Help with OOP

 9:03 am on Jan 3, 2011 (gmt 0)

class myclass {
public myvar1;
public myvar2;

function __construct() {
if ($his->myvar1 == "a") {
$this->myvar2 = "A";
} else {
$this->myvar2 = "B";

function show_var() {
return $this->myvar2;



$mytest = new myclass();
$mytest->myvar1 = "a";

echo $mytest->myvar1;// Result a
echo $mytest->myvar2;// Result B


Why Im getting a result of "B" for $this->myvar2?
I should get A since Ive modified the value of myvar1 to "a" in my index.php file and based on the condition if myvar1 = "a" myvar2 should return a value of "A".

Please Help.



 9:53 am on Jan 3, 2011 (gmt 0)

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:-

//error reporting

//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";
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.



 10:02 am on Jan 3, 2011 (gmt 0)

Hi Matthew1980,

Thanks for that detailed explanation.

Now how can I modify the value of myvar1 to "a" from my index.php so that myvar2 will give me a value of "A".


 10:41 am on Jan 3, 2011 (gmt 0)

>>Now how can I modify the value of myvar1 to "a" from my index.php so that myvar2 will give me a value of "A".

Well, you can attach another variable to that so that the property becomes variable, this can be driven from a switch able constant, or a database field, that's your choice, The mechanics of that function now, I just popped it all into one file so that I was sure that it was working the way I expected it to.

What ever variable you declare AFTER the class instantiation can be overridden from the var declaration in the class object, caught me a few times! So as long as you have the variable in the class defined with no value: var $myvar; and not var $myvar = ""; then what ever is declared when you instantiate is the on that takes precedence.

Oh eck, confusing myself, but yes, that's about right. Apologies if I have misunderstood your question.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved