JDSolutions
JDSolutions

Reputation: 115

Replacing text to class function

I'm came with one problem and thinking is there any solution of my programming way. I hope everyone will understand problem and might help.

I have html template let's say like that:

<title>{Content~title}</title>
</head>
<body>
    {Content~Display("testas")}
</body>

And I have a class called Content:

class Content {

    public $title = "Test";

    public function __construct(){}

    public function Display($text){
        return $text;
    }       
}

Now, I have script which load class and puts in array like that:

$this->objects[$class] = new $class(); // in this case: Content class

After that I'm trying to replace my tags {Content~title} and {Content~Display("testas")} via this code:

$replaceContent = $this->objects[$class_name];
$content = str_replace($current,$replaceContent->{$key},$content); // key- Display("testas")

With title (because it's class property) works good, but with function I came with error:

Notice:  Undefined property: Content::$Display("testas") ..

However if I'm trying to use like:

echo $replaceContent->Display("TEST");

Work's fine... any ideas how to replace my tag to function content from class?

Upvotes: 0

Views: 51

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

You should not put the whole part into key variable. You should have 2 variables for example:

$functionName = 'Display';
$functionArg = 'testas';

and than:

$replaceContent->$functionName($functionArg)

Upvotes: 1

moonwave99
moonwave99

Reputation: 22817

You are trying to access a non-existing field of the class [as the error states].

As you have a function, you have to invoke it:

$replaceContent->{$key}( $content )

This will call your Display() function, which should be named in lower case by the way, and should do something more than just retuning its parameter I guess.

Upvotes: 1

Related Questions