Jump to content

[SOLVED] Variables in a string inputted into Class Method


WBSKI

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.