PHP 在函数的函数中设置变量

PHP setting variable inside function's function

我需要有关设置变量的建议。我正在使用 Slim Framework。我有几个 类.

MailboxManager.php

    public function getMailById($id) {
        /* @var $sql PDOStatement */
        $sql = $this->database->prepare("SELECT * FROM appmail_mails WHERE id = :id");
    $sql->bindParam('id', $id);
    $sql->execute();
    $record = $sql->fetch(PDO::FETCH_ASSOC);
    if ($record === false) {
        return null;
    }
    $mail = new Mail($id);
    $mail->setMailContent($record['content']);
    $mail->setMailDate($record['date']);
    $mail->setMailSubject($record['subject']);
    $mail->setMailSize($record['
return $mail;

MailController.php

class MailController
private $id;
public function showMail()
    $mail = ServicesFactory::getMailboxManager()->getMailById($this->id);
    echo "<pre>";
    var_dump($mail);

现在我想用 Slim Framework 创建路径。在索引 php 中,我输入:

$app->get('/dupa/{id}', function(ServerRequestInterface $request){

    $id = $request->getAttribute('id');
    echo $id;
    $new = new MailController();
    $test = $new->showMail();
    var_dump($test);
});

我应该如何设置 id:

$mail = ServicesFactory::getMailboxManager()->getMailById($this->id);

正确获取? 每次我尝试:

$test = $new->showMail($id)

我刚得到 null。

谢谢你的帮助。

路由参数作为闭包的第三个参数传递

$app->get('/dupa/{id}', function(ServerRequestInterface $request, $response, $args){

    $id = $args['id'];
    echo $id;
    $new = new MailController();
    $test = $new->showMail();
    var_dump($test);
});