Jump to content

[SOLVED] OOP question


allworknoplay

Recommended Posts

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;
?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/145490-solved-oop-question/
Share on other sites

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;

 

:-)

Link to comment
https://forums.phpfreaks.com/topic/145490-solved-oop-question/#findComment-763811
Share on other sites

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?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/145490-solved-oop-question/#findComment-763814
Share on other sites

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/145490-solved-oop-question/#findComment-763825
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/145490-solved-oop-question/#findComment-763835
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.