Object->Object[n]->property don´t work

Hello:

I have this code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head><title>Untitled</title></head>
<body>

<?
      class A {                          var $aa;
      }  // class A
      
      class B {                         var $bb;
      }  // class B
      
      $X = new B;
      $X->bb[0] = new A;
      $X->bb[0]->aa = "OK";
      
      echo $X->bb[0]->aa;
?>

</body>
</html>

But I get this:

"Parse error: parse error in clasedeclase.php3 on line 21"

Of course, line 21 is:
$X->bb[0]->aa = "OK";

How must I to write this code?

I write this code with ('s and {'s but don´t work.

Thanks,
Oscar =;-))

LVL 1
oruedaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gravityCommented:
Try closing the classes with a ';' e.g.

Class A() {
//class bits
}; //note the ';'
0
gravityCommented:
Is it important to use $bb as an array (i.e. with the index of [0]) ? If not then this should work :

<?
 
class A {    
      var $aa;
};

class B {
      var $bb;
};

$X = new B;
$X->bb = new A;
$X->bb->aa="OK";
echo $X->bb->aa;
?>
0
maxkirCommented:
This works for me:

                   class A {    var $aa;
                   }  // class A

                   class B { var $bb;
                   }  // class B

                   $X = new B;
                   $X->bb[0] = new A;
                   $t = $X->bb;
                   
                  $t[0]->aa= "OK";

                   echo $t[0]->aa;
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

oruedaAuthor Commented:
Closing the classes with a ';' don´t work.

I need a array of object in a object.

Create temporal object is bad because the original object don´t have the values and the code is ugly.

Any solutions?

Thanks
Oscar =;-))
0
maxkirCommented:
This code a little bit ugly too, but values in object array ARE set. The trick is to pass object array by reference.

                 class A {    var $aa;
                   }  // class A

                   class B { var $bb;

                   function set_value($obj_array, $index, $value)
                   {
                       $obj_array[$index]->aa = $value;
                   }


                   }  // class B

                   $X = new B;
                   $X->bb[0] = new A;
                   $X->set_value(&$X->bb, 0, "OK");

                   $t = $X->bb;
                   echo $t[0]->aa;

0
oruedaAuthor Commented:
I find this http://bugs.php.net/bugs.php3?id=2826&edit=1 and other similars.

Oscar =;-((





0
maxkirCommented:
Does this workaround help you ?

 If use it, original objects in array receive values ...
 This code is less ugly,IMHO ;-)

class A {    var $aa;
  }  // class A

class B {
  var $bb;

  /* public */
  function get_value($index)
  {
      $t = $this->bb;
      return $t[$index]->aa;
  }

  /* public */
  function set_value($index, $value)
  {
      $this->private_set_value(&$this->bb, $index, $value);
  }
 
 
  /* !! private !! */
  function private_set_value($obj_array, $index, $value)
  {
      $obj_array[$index]->aa = $value;
  }


  }  // class B

  $X = new B;
  $X->bb[0] = new A;
  $X->set_value( 0, "OK");
  echo $X->get_value(0);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.