WBSKI Posted April 9, 2008 Share Posted April 9, 2008 I am not sure what the term is for this so I will do my best to explain my problem. I am calling a method which prints out a mysql table, one variable is required by the function: $template $template looks like this: "<p>$row[name]</p> So the method call would look like this: $theclassobject->echotabletemplated("<p>$row[name]</p> The problem is that I want the $row to be the referencing variable within the method, rather than the $row variable from where the method is called, which is not in the same class. Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/100264-solved-variables-in-a-string-inputted-into-class-method/ Share on other sites More sharing options...
trq Posted April 9, 2008 Share Posted April 9, 2008 Its possible, but not at all pretty. Firstly, you need to make sure that the argument passed to echotabletemplated() is a single quoted string so it won't be interpolated from within the calling code. There is no way to inforce this in php or any other language I'm aware of. Once that is done, you can use http://php.net/eval]eval[/url]() to parse the passed in string as php within the echotabletemplated() method. Having pointed out how it may be possible I would urge you to rethink your design. This is never going to hold water. Quote Link to comment https://forums.phpfreaks.com/topic/100264-solved-variables-in-a-string-inputted-into-class-method/#findComment-512663 Share on other sites More sharing options...
Cobby Posted April 9, 2008 Share Posted April 9, 2008 So is $row a property inside the class? <?php class foo { public function echo($var){ echo $this->{$var}; } } $foo = new foo(); $foo->echo('testVar'); // will display $foo->testVar ?> Cobby Quote Link to comment https://forums.phpfreaks.com/topic/100264-solved-variables-in-a-string-inputted-into-class-method/#findComment-512830 Share on other sites More sharing options...
WBSKI Posted April 9, 2008 Author Share Posted April 9, 2008 $row is within the method called Quote Link to comment https://forums.phpfreaks.com/topic/100264-solved-variables-in-a-string-inputted-into-class-method/#findComment-512996 Share on other sites More sharing options...
WBSKI Posted April 11, 2008 Author Share Posted April 11, 2008 Ok, the eval worked perfectly. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/100264-solved-variables-in-a-string-inputted-into-class-method/#findComment-514442 Share on other sites More sharing options...
Jenk Posted April 16, 2008 Share Posted April 16, 2008 I would strongly reconsider your design implementation. You should not know what properties are within an object if they are not public. Quote Link to comment https://forums.phpfreaks.com/topic/100264-solved-variables-in-a-string-inputted-into-class-method/#findComment-518362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.