PHP "Undefined Function"即使它实际上在同一个文件中

标签 php function email cakephp

我在这个特定的应用程序上使用 CakePHP。通过 add() 在网站上提交博客文章时,它应该通过 pending_email() 向管理员发送通知电子邮件,但我收到以下信息错误:

fatal error :调用 .../app/controllers/blog_posts_controller.php 中的未定义函数 pending_email() 第 134 行

代码如下:

function add($blog_id = null) {
    if (!empty($this->data)) {
        switch ($this->params['form']['submit']) {
            case 'draft':
                $this->data['BlogPost']['status_id'] = DRAFT;
                break;
            case 'approval':
                $this->data['BlogPost']['status_id'] = PENDING;
                break;
            case 'publish':
                $this->data['BlogPost']['status_id'] = PUBLISHED;
                break;
        }
        $this->data['BlogPost']['tags'] = $this->__tagCleanup($this->data['BlogPost']['tags']);
        $this->BlogPost->create();
        if ($this->BlogPost->save($this->data)) {
            if ($this->data['BlogPost']['status_id'] == PUBLISHED) {
                $this->__tagUp($this->data['BlogPost']['blog_id'], $this->data['BlogPost']['tags']);
            }

            // Send the e-mail notating that a blog is pending
            pending_email($blog_id);          

            $this->Session->setFlash(__('The blog post has been saved', true));
            $this->redirect(array('action' => 'index', $this->data['BlogPost']['blog_id']));
           } else {
            $this->Session->setFlash(__('The blog post could not be saved. Please, try again.', true));
        }
    }
    // fetch blog_post context
    $this->BlogPost->Blog->recursive = -1;
    $blog = $this->BlogPost->Blog->read(array('id','title'),$blog_id);
    $this->set('blog', $blog);

    $this->data['BlogPost']['user_id'] = $this->user_id;
    if (!is_null($blog_id)) {
        $this->data['BlogPost']['blog_id'] = $blog_id;
    }


}


function pending_email($id) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid blog post', true));
            $this->redirect(array('action' => 'index'));
        }

        $this->BlogPost->contain(array(
            'User' => array('fields' => 'id', 'full_name', 'name', 'last_name'),
            'Tag' => array('fields' => 'name'),
            'BlogPostComment' => array('fields' => array('created', 'content')),
            'BlogPostComment.User' => array('fields' => array('name', 'last_name')),
        ));

        if(($blogPost = $this->BlogPost->read(null, $id)) == NULL){
            $this->Session->setFlash(__('Invalid blog post', true));
            $this->redirect(array('controller'=>'spotlights', 'action' => 'index'));
        }

        $this->set('blogPost', $this->BlogPost->read(null, $id));
        $temp = $this->BlogPost->read(null, $id);
        $this->BlogPost->Blog->recursive = -1;

        //Blog information
        $title = $temp['BlogPost']['title'];
        $author = $temp['User']['full_name'];
        $date_created = $temp['BlogPost']['created'];

        // Properly format the excerpt
        $excerpt = preg_replace("/&#?[a-z0-9]+;/i","", $temp['BlogPost']['content']);

        $content = "<h2>Hello,</h2><p>$author has submitted a new blog post for review. <ul><li><b>Title:</b> $title </li><li><b>Author</b>: $author</li><li><b>Excerpt</b>: \"$excerpt\"</li><li><b>Created on:</b> $date_created</li></ul></p><p>You can log into the dashboard to approve this post.</p>";
        $to = ADMIN_EMAIL;
        $from = SMTP_FROM;
        $subject = "New blog post submitted by $author";
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: ' . $from . "\r\n" .
        'Reply-To: ' . $from . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

        //Send the e-mail
        mail($to, $subject, $content, $headers);
}  

出于隐私考虑,我删除了一些内容,但不应该涉及这个问题。一如既往,非常感谢任何想法!

最佳答案

由于pending_email不是全局函数,而是 Controller 上的一个方法,所以需要这样调用:

$this->pending_email($blog_id);

$this 关键字引用博客文章 Controller 类。

关于PHP "Undefined Function"即使它实际上在同一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037405/

相关文章:

PHP:正则表达式匹配字符串前面没有数字或空格

php - JSON NSDictionary 在输出到日志时显示空描述

java - Servlet 发送请求后未正确响应

php 邮件在hotmail 中总是变成垃圾邮件

javascript - 如何在 JQuery 中使用 foreach 循环

PHP读取对象属性

php - 尝试使用函数调用sql查询时的警告消息和通知

c++ - 定义类(和嵌套类)成员函数的更好/更整洁/更可读的方法?

c# - 如何从 ASP.NET 发送大量电子邮件?

sql - 使用 SP_SEND_DBMAIL 的存储过程向所有收件人发送重复的电子邮件