Hi,

Wednesday, October 1, 2003, 9:00:18 AM, you wrote:
GH> I'm working for the first time with object orientated programming in php and
GH> I can't figure out how to access elements or methods when you place objects
GH> inside objects inside other objects.

GH> my origonal idea was to  use the following syntax:
$a->>b->c
GH> but this just returns:
$a->>b  . "->c"

GH> Please, need advice on the finer points of OOP in PHP


if you have this

class a {
  var $var_a = 'A';
  function a ($var){
    $this->$var_a = $var;
  }
  function get_var(){
    return $this->var_a;
  }
}
class b {
  var $a = '';
  function b ($var){
    $this->$a = new a($var);
  }
  function get_var(){
    return $this->a->get_var();
  }
}

$class_b = new b("Hello');

You can access class a directly with

$class_a_var = $class_b->a->var_a;

but this relies on you knowing the structure of your classes. (note we drop the
$ after the ->)

What is better is to use the black box method and make access functions in each
class and do

$class_a_var = $class_b->get_var();

This way we can change how class a works and not worry about how it stores its
data. With this method if we change the variable name in class a to $new_a the
code will still work, where as $class_b->a->var_a will now fall in a heap.

Hope this helps
-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to