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

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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