allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 Sorry for asking this, when I run it, the output is "5". But how is that possible? It seems like horrible code...then again I am still learning OO myself.. Could someone please explain to me how this code even works? <?php $global_obj = null; class my_class { var $value; function my_class() { global $global_obj; $global_obj = &$this; } } $a = new my_class; $a->my_value = 5; $global_obj->my_value = 10; echo $a->my_value; ?> Quote Link to comment Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 The class you are using does nothing else then give the value you give here you are starting the class $a = new my_class; Here your giving him a value to play with $a->my_value = 5; (return teh same value This row does nothing $global_obj->my_value = 10; this row echos the value you gave to the class echo $a->my_value; :-) Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Author Share Posted February 17, 2009 The class you are using does nothing else then give the value you give here you are starting the class $a = new my_class; Here your giving him a value to play with $a->my_value = 5; (return teh same value This row does nothing $global_obj->my_value = 10; this row echos the value you gave to the class echo $a->my_value; :-) Thanks 2 questions.. 1) Should PHP output some kind of warning or error for this? $global_obj->my_value = 10; I mean, the "$global_obj->" doesn't even exist, yet alone allow you to set it a value to 10. 2) $a->my_value = 5 The class "my_class" has no property called "my_value", so I thought it should output an error? I thought properties had to be declared in classes before you can use them, let alone try to set a value? Is PHP implicitly creating the "my_value" for you in the "my_class" class? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 1) it does but this is not a critical error so the script continues to see the error put this on top of your page error_reporting(E_ALL); 2) Yes but you are giving him a value $a->my_value = 5; so what ever you do, it's always gona assume $a->my_value is equal to 5 Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Author Share Posted February 17, 2009 1) it does but this is not a critical error so the script continues to see the error put this on top of your page error_reporting(E_ALL); 2) Yes but you are giving him a value $a->my_value = 5; so what ever you do, it's always gona assume $a->my_value is equal to 5 Ok thanks, I tried adding: error_reporting(E_ALL); And I ran it but it didn't output any error....I am using PHP 5.2.8 if that helps? Anyways, doesn't really matter I was just curious... Ok so now I understand, even if a property doesn't exist in a class, if I make it up on the fly: $a->whatever = 10 $a->something = "hello" Then PHP will make it part of the "$a" object right? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 Yeah try outputing the result to check it out echo '<pre>'; print_r($a); echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Author Share Posted February 17, 2009 Yeah try outputing the result to check it out echo '<pre>'; print_r($a); echo '</pre>'; Yes you're right, I just ran it and it added values on the fly to the object. You know, the past 2 weeks, I've read about 3-4 books on OOP and none of them taught me about this little trick... Quote Link to comment Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 hehe yeah they tell a lot of stuff on a lot of stuff but they always forget the details ... hehe Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.