[SOLVED] Simple OO question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Simple OO question

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try:

Code: Select all

$v="_tel['test']";
$temp->$$v->set_names('testing');
$temp->${$v}->set_names('testing');
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post by myleow »

None of those method work.
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post 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.
Post Reply