kasimir
kasimir

Reputation: 1554

Accessing Markdown PHP function from within a class

I'm using the Markdown library for PHP by Michel Fortin. Setup is easy and it works great like this:

include_once "markdown.php";
$my_html = Markdown($my_text);

However, I have a class in which I want to pass stuff and 'Markdown' it, like so:

class Test
{
    public function showMarkdown ($text)
    {
        return Markdown($text);
    }
}

Obviously, my class is much larger than this, but this is what it boils down to. In my main script I do:

include_once "markdown.php";
$test = new Test();
echo $test->showMarkdown($text);

This returns an error, saying the function 'Markdown' is undefined. That seems obvious, because it's not within the class and I haven't used a scope operator. But when I put the include inside my class and use $this->Markdown or self::Markdown the function is still undefined. I figured that the Markdown function can't be defined inside another function.

So, how can I solve this? I need to do the include, which loads the Markdown function (and the rest of its family) but I want to be able to use it from within my classes.
Thanks for your answers/ideas.

Upvotes: 0

Views: 204

Answers (2)

Sonal Khunt
Sonal Khunt

Reputation: 1894

write

function showMarkdown ($text)
in place of
public function showMarkdown ($text)

and

echo $test->showMarkdown("Hello World");

Upvotes: 0

Jon
Jon

Reputation: 437454

Your example code calls a free function called Markdown (which presumably is defined in markdown.php). You simply need to put the include in the same file as your Test class.

After doing this, you will still call Markdown as a free function, and not as an instance ($this->Markdown) or static (self::Markdown) method.

Upvotes: 1

Related Questions