glenelkins Posted October 10, 2008 Share Posted October 10, 2008 Hi Im writing an app and I would like to create a class with a name that is specified either in a database or a variable. Is this possible? for example, can something like this be done? $class_name = "test"; class $class_name { } This code doesnt work, but I was wondering if there was a way to do this? Also, Iv seen many of times variables set like this in a string echo "Hello my name is {$name}" ... what exactly are the {} for? they are not needed so why are they there? Thanks Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/ Share on other sites More sharing options...
thebadbad Posted October 10, 2008 Share Posted October 10, 2008 I don't know about the variable class name, but {} are required when you wanna access an array element with a string as key, within quotes: <?php $array = array('foo' => 'bar'); echo "foo{$array['foo']}"; ?> Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661797 Share on other sites More sharing options...
thebadbad Posted October 10, 2008 Share Posted October 10, 2008 Another use is, when you wanna use the right variable: <?php $foo = 'bar'; $foobar = 'foobar'; echo "$foobar"; //foobar echo "{$foo}bar"; //barbar ?> Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661798 Share on other sites More sharing options...
glenelkins Posted October 10, 2008 Author Share Posted October 10, 2008 ok so another one, can a function be created like this: ? $function = "test"; $function() { } thebadbad, i dont see the point in your example, you could simply use echo "$array[foo]"; Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661800 Share on other sites More sharing options...
thebadbad Posted October 10, 2008 Share Posted October 10, 2008 A third use is for variable variables: <?php $a = 'foo'; $b = 'bar'; $foobar = 'foo'; echo "${$a . $b} bar"; //foo bar echo ${$a . $b} . ' bar'; //foo bar ?> And I'm sure there's more.. To reply, yes, you can. Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661803 Share on other sites More sharing options...
glenelkins Posted October 10, 2008 Author Share Posted October 10, 2008 yes you can to what? the function? Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661804 Share on other sites More sharing options...
thebadbad Posted October 10, 2008 Share Posted October 10, 2008 echo "$array[foo]"; will throw a warning (if turned on). String keys should always be enclosed in quotes. Yes, the function: <?php function test() { echo 'test'; } $name = 'test'; $name(); //test ?> Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661805 Share on other sites More sharing options...
glenelkins Posted October 10, 2008 Author Share Posted October 10, 2008 No you have what im saying wrong. I know i can call a function like this function test(){ } $function = "test"; $function(); What im saying is can a function be "created" the same way? Like so: $function_name = "test"; // Create the function $function_name(){ } Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661810 Share on other sites More sharing options...
thebadbad Posted October 10, 2008 Share Posted October 10, 2008 Oh, sorry, my bad. Why don't you try? Link to comment https://forums.phpfreaks.com/topic/127831-is-this-possible/#findComment-661816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.