<?php
class A {
var $x;
var $y;
}

$a = new A;
$a->$x = 10;
$a->$y = 20;

echo "x=". $a->$x;
echo "y=". $a->$y;

?>

take a look at the program code carefully and run it,

and you'll get these x=20 and y=20.

i'm not so sure but don't you think the output must be x=10 and y=20?

    Take the dollar signs out of your class property references:

    class A {
    var $x;
    var $y;
    }
    
    $a = new A;
    $a->x = 10;
    $a->y = 20;
    
    echo "x=". $a->x;
    echo "y=". $a->y;
    

      class A {
      var $x;
      var $y;
      }

      $a = new A;
      $a->$x = 10;
      $a->$y = 20;

      echo "x=". $a->$x;
      echo "y=". $a->$y;

      ?>

        I tried it but i'm receiving an error message

          Try enabling error_reporting(E_ALL), then you will see the notices telling you about the use of uninitialised variables which is the source of your problem.

          In particular,

           $a->$x = 10;
          

          Is not the right syntax for what you're trying to do, and in fact does something completely different.

          Mark

            Yes. Instead of $a->$x = 10; you need $a->x = 10;

            Look carefully again at Mark's post and mine.

              I already solved the problem now. That syntax ($a->$x) works on version 5.0 but when i installed the latest version of php 5.1.6, the syntax doesn't work anymore, i'm not sure maybe they changed the syntax because in my book the syntax is $a->$x and it also works fine on version 5.0. Not so sure but i think it's a bug in PHP 5.0. Thanks for you help guys.

              btw, sorry for my english, i'm good at it.

                No, it is NOT a bug. It's just that $a->$x does not do what you (still) think it does.

                $a->$x accesses the property of $a named by $x. This is not what you're trying to do - you mean $a->x which is something totally different.

                If $x is undefined, $a->$x may access a property named by the empty string "" on $a. This is not a bug either, this is what PHP does if you choose to ignore the notice about an undefined variable and do this anyway.

                Mark

                  Here's my first post

                  class A {
                  var $x;
                  var $y;
                  }

                  $a = new A;
                  $a->x = 10;
                  $a->y = 20;

                  echo "x=". $a->x;
                  echo "y=". $a->y;

                  and here's my second post

                  class A {
                  var $x;
                  var $y;
                  }

                  $a = new A;
                  $a->$x = 10;
                  $a->$y = 20;

                  echo "x=". $a->$x;
                  echo "y=". $a->$y;

                  If you noticed i used two different version but the output is the same. I'll be back for this, i'm busy right now

                    I mean, "btw, sorry for my english, i'm not good at it".

                    Did you try to code that in version 5.0.0? I'm not so sure if you are aware of this but there's a bug on version 5.0.0

                      1. You posted the same code in both the posts in this thread. And the code was with $a->$x = 10;.

                      2. As people have tried to tell you it should not work with that code. It is no bug. Don't you think that someone else would have noticed if it was a bug? And if it would be a bug, why are you posting it here, it would be better to report to the guys developing PHP.

                      3. I can't understand the problem at all. If you have troubles with version 5.0.0 then don't use it. Simple.

                        Yeah. Piranha and MarkR are right. You are missing a very important and subtle detail. There isn't a bug with PHP unless it's a faulty neuron path buried deep in the header files of the codewriter. I'd have to check CVS to be sure...

                        Version 5.0.0 is no longer supported anyway. Upgrade to 5.1.6

                          I already said that i already solved the problem by installing the latest version and that fixed the problem (don't you think its a bug). In my second post, I changed it to $a->x but i noticed i posted the wrong code (sorry for that). When i coded that in version 5.0, i tried both of these $a->x and $a->$x and the output is still the same, i didn't see any error message, if there's i'll see that and post it here. If you didn't know version 5.0 has a memory issue and there are a lot of people asking about that bugs (maybe your using apache that's why you didn't see the bug in version 5.0), i always seeing that error message when running the script but i already forgot that error message i wish i can post it here. Even a simple script like this,

                          <?php
                          ?>

                          you'll see an error message about memory.

                          I posted it here because i'm not so sure that's why i'm asking for someone's help and i also i thought i was using the latest version but i was wrong, i checked there site and found version 5.1.6 and that fixed the problem.

                            Write a Reply...