Forum Moderators: coopster

Message Too Old, No Replies

variable class name

what's the correct syntax for: $a = new $b.'_helper'()?

         

idev

6:36 am on Mar 27, 2009 (gmt 0)

10+ Year Member



Guys, I need your help.

Looks simple, but can't figure out the best way to do that other than having a temporary variable.

This is the sample code that works:


<?php

class A_test {
function b() {
echo 'it worked';
}
}

$a = new A_test();
$a->b();

This is the same with dynamic helper name, that I want to achieve:


<?php

class A_test {
function b() {
echo 'it worked';
}
}

$mode = 'A';

$a = new $mode.'_test'();
$a->b();

any way to make the parser understand what I want?

P.S. new ${$mode.'_test'}() didn't work.

eeek

11:34 pm on Mar 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$a = new $mode.'_test'();

Try this:

$a=new A_test;

idev

12:09 am on Mar 28, 2009 (gmt 0)

10+ Year Member



Thanks for the attention, eeek, but please read the original request completely before you respond.

I still could find no way of doing that, so I went with a temporary variable:

$tmp = $mode.'_test';
$a = new $tmp();

I bet there's no other way w/ php5