【问题标题】:Void as a str_replace() param in PHP 8 [closed]void 作为 PHP 8 中的 str_replace() 参数 [关闭]
【发布时间】:2021-11-05 08:28:27
【问题描述】:

我正在尝试将我的小项目迁移到 PHP 8,但我正在努力使用相当于 str_replace() 的函数。

public function renderView($view)
{
    $layoutContent = $this->layoutContent();
    $viewContent = $this->renderOnlyView($view);
    return str_replace('{{content}}', ($viewContent), strval($layoutContent));
}

protected function layoutContent()
{
    ob_start();
    include_once Application::$ROOT_DIR."/views/layouts/main.php";
    ob_get_clean();
}


protected function renderOnlyView($view)
{
    ob_start();
    include_once Application::$ROOT_DIR."/views/$view.php";
    ob_get_clean();
}

问题是在 PHP 8 中不能在 str_replace() 函数中使用 $view 并且我得到:Expected type 'array|string'. Found 'void'

有没有人可以解决这个问题?

【问题讨论】:

  • 您的 renderOnlyView 函数没有返回任何内容,并且您没有将任何内容 (void) 添加到 str_replace。你到底想达到什么目的?
  • 所以我有一个 template.php,它包含一个页眉、导航栏、页脚,在里面我有一个 {{content}} 字符串要替换为 contact-template.php 的值, home-page.php 等
  • 问题是你没有将任何东西传递给 str_replace(),正如@AdamBaranyai 所说。
  • 为了清楚起见,在任何 PHP 版本中,显示的代码永远不会完成您所描述的操作str_replace 函数没有被传递任何东西,因为 layoutContentrenderOnlyView 都没有返回值。我投票关闭“需要调试细节”,因为如果您有在 PHP 7 中工作的代码,那不是您向我们展示的代码。可能,这段代码一直被破坏,而 PHP 8 只是在指出它方面更有帮助。

标签: php php-8


【解决方案1】:

在不确切知道代码的其他部分的情况下,这只是我的猜测,但请尝试修改您的 renderOnlyViewlayoutContent 函数,如下所示:

public function renderView($view)
{
    $layoutContent = $this->layoutContent();
    $viewContent = $this->renderOnlyView($view);
    return str_replace('{{content}}', $viewContent, $layoutContent);
}

protected function layoutContent()
{
    ob_start();
    include_once Application::$ROOT_DIR."/views/layouts/main.php";
    $out = ob_get_clean();

    return $out;
}

protected function renderOnlyView($view)
{
    ob_start();
    include_once Application::$ROOT_DIR."/views/$view.php";
    $out = ob_get_clean();

    return $out;
}

这应该分别从您的Application::$ROOT_DIR."/views/$view.php"Application::$ROOT_DIR."/views/layouts/main.php" 文件中捕获每个echo-ed 信息,并将其作为字符串返回给调用者范围。

【讨论】:

  • public function renderView($view) { $layoutContent = $this->layoutContent(); include_once Application::$ROOT_DIR."/views/$view.php"; } protected function layoutContent() { include_once Application::$ROOT_DIR."/views/layouts/main.php"; } 直到我的函数看起来像这样并且未使用 renderOnlyView 它才有效,但在文件中间返回 {{content}}。您的更改没有任何作用。您需要查看代码的任何确切部分吗?
  • Adam Baranyai 函数看起来不错。我不相信他的改变没有任何作用。你仍然得到预期类型'array|string'。找到“无效”?
  • 这两个包含很可能是回显输出,您试图在问题中的代码的当前版本中捕获这些输出,然后将其丢弃。我的修改应该捕获这些输出,并将它们传递出去。我对您的第一个功能进行了另一次修改,也尝试一下,并告诉我它现在是否有效。 (我不熟悉将变量包装在括号中,但我再次使用 PHP 7 而不是 8,所以也许这是新事物)
  • 另外,如果错误因为我的更改而消失,但您仍然看到空白屏幕,这意味着您没有在任何地方回显renderView 函数的输出。在这种情况下,将return str_replace(...) 更改为echo str_replace(...)
【解决方案2】:

如果您设置了strict_types = 1,那么您必须将参数作为数组传递

public function renderView($view)
{
    $layoutContent = $this->layoutContent();
    $viewContent = $this->renderOnlyView($view);
    return str_replace(['{{content}}'], [$viewContent], strval($layoutContent));
}

您可以阅读它here

【讨论】:

  • 不幸的是,我仍然得到一个空白页,请检查我上面的评论,有一个代码可以工作但不能替换 {{content}}
  • 如果我返回 layoutContent()renderOnlyView($view) 的 include_once 也同样有效,但我仍然需要在此处替换 {{content}} 字符串
  • 这个答案完全错误:str_replace 接受字符串或数组参数
  • @ManojKiranAppathurai 这和我说的一样:它需要一个字符串或一个数组。 OP 试图传递一个完全有效的字符串,但在其他地方有一个错误,这意味着它们传递的是 null。将 null 包装在数组中并不能解决问题,尽管如果 PHP 不检查数组中的类型,它可能会隐藏它。
猜你喜欢
  • 1970-01-01
  • 2021-07-30
  • 2013-12-14
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 2013-03-31
相关资源
最近更新 更多