从类中访问 Markdown PHP 函数
我正在使用 Michel Fortin 的 PHP Markdown 库。设置很简单,而且效果很好,如下所示:
include_once "markdown.php";
$my_html = Markdown($my_text);
但是,我有一个类,我想在其中传递内容并对其进行“Markdown”,如下所示:
class Test
{
public function showMarkdown ($text)
{
return Markdown($text);
}
}
显然,我的类比这个大得多,但这就是它的归结。在我的主脚本中,我这样做:
include_once "markdown.php";
$test = new Test();
echo $test->showMarkdown($text);
这会返回一个错误,指出函数“Markdown”未定义。这似乎很明显,因为它不在类内,而且我没有使用范围运算符。但是当我将 include 放入类中并使用 $this->Markdown
或 self::Markdown
时,该函数仍然未定义。我认为 Markdown 函数不能在另一个函数中定义。
那么,我该如何解决这个问题呢?我需要执行包含操作,它会加载 Markdown 函数(及其系列的其余部分),但我希望能够在我的类中使用它。
感谢您的回答/想法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例代码调用一个名为
Markdown
的免费函数(可能是在markdown.php
中定义的)。您只需将包含内容与您的Test
类放在同一文件中即可。执行此操作后,您仍然会将
Markdown
作为自由函数调用,而不是作为实例 ($this->Markdown
) 或静态 (self:: Markdown
) 方法。Your example code calls a free function called
Markdown
(which presumably is defined inmarkdown.php
). You simply need to put the include in the same file as yourTest
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.写
和
write
and