Page 1 of 1

Simple OO question

Posted: Thu Jan 06, 2005 9:12 pm
by myleow
I am trying to use variable variable names in a method call. The name is an array.

When i try

Code: Select all

$temp->_telї'test']->set_names('testing');
it works fine but when i try

Code: Select all

$v="_telї'test']";
$temp->{$v}->set_names('testing');
I get
Fatal error: Call to a member function set_names() on a non-object
I can use the second example if my variable is not an array, example $v=test.

Posted: Thu Jan 06, 2005 9:14 pm
by feyd
try:

Code: Select all

$v="_tel['test']";
$temp->$$v->set_names('testing');
$temp->${$v}->set_names('testing');

Posted: Thu Jan 06, 2005 9:19 pm
by myleow
None of those method work.

Posted: Thu Jan 06, 2005 9:24 pm
by myleow
php5.

Well like i say

Code: Select all

$v=test;
$temp->{$v}->set_names('testing');
works fine.

Code: Select all

$temp->_tel['test']->set_names('testing');
works fine

Code: Select all

$v="_tel['test']";
$temp->{$v}->set_names('testing');
Does not Work

Posted: Thu Jan 06, 2005 9:43 pm
by markl999
Ah i see what you mean now.
The closest i could get was the below and i'm not sure even that comes close enough for you .. it will depend.

Code: Select all

class foo {
  public $cheese;
  function foo(){
    $this->cheese['eek'] = new bar();
  }
}
class bar {
  function test(){
    echo 'in test';
  }
}
$v = 'eek';
$temp = new foo();
$temp->cheese[$v]->test();
PHP5's __get function might help but i couldn't get it to work exactly as you wanted.

Posted: Thu Jan 06, 2005 10:25 pm
by myleow
Its ok, i gave up on the Object referencing a pass by reference array. I think the memory location got screwed up and i am lazy to figure out the error.