致命错误:声明[不同]时调用未定义的函数

时间:2018-02-13 19:00:39

标签: php fatal-error

嗯,经过研究后我才发现this question和一个drupal的事情。 我发现的问题与我的不同,因为他们的错误是在if中声明函数,而不是那里。

让我们直截了当。我正在使用一个类(classes / Post.php)来显示一些帖子。如果我打电话给我,我会收到这个错误:

致命错误:在第239行的C:\ xampp \ htdocs \ classes \ Post.php中调用未定义的函数timeCounting()

这是我的Post.php类(第一个函数是timeCounting():

<?php

include('./classes/Notify.php');

class Post {

    public static function timeCounting($time_ago)
    {
        $time_ago = strtotime($p['posted_at']);
        $cur_time   = time();
        $time_elapsed   = $cur_time - $time_ago;
        $seconds    = $time_elapsed ;
        $minutes    = round($time_elapsed / 60 );
        $hours      = round($time_elapsed / 3600);
        $days       = round($time_elapsed / 86400 );
        $weeks      = round($time_elapsed / 604800);
        $months     = round($time_elapsed / 2600640 );
        $years      = round($time_elapsed / 31207680 );

        if ($seconds <= 60) {
            return "just now";
        }
        else if ($minutes <= 60) {
            if ($minutes == 1) {
                return  "an minute ago";
            } else {
                return "$minutes minutes ago";
            }
        }
        else if ($hours <= 24) {
            if ($hours == 1) {
                return  "an hour ago";
            } else {
                return "$hours hours ago";
            }
        }
        else if ($days <= 7) {
            if ($days == 1) {
                return  "yesterday";
            } else {
                return "$days days ago";
            }
        }
        else if ($weeks <= 4.3) {
            if ($weeks == 1) {
                return  "a week ago";
            } else {
                return "$weeks weeks ago";
            }
        }
        else if ($months <= 12) {
            if ($months == 1) {
                return  "a month ago";
            } else {
                return "$months months ago";
            }
        }
        else if ($years <= 1) {
            if ($years == 1) {
                return  "a year ago";
            } else {
                return "$years years ago";
            }
        }
        else if ($years >= 10) {
            if ($years == 10) {
                return  "a decade ago";
            } else {
                return "over than a decade ago ($years years)";
            }
        }
    }

    public static function createPost($postbody, $loggedInUserId, $profileUserId) {

            if (strlen($postbody) > 160 || strlen($postbody) < 1) {
                    die('Incorrect length!');
            }

            $topics = self::getTopics($postbody);

            if ($loggedInUserId == $profileUserId) {

                    if (count(Notify::createNotify($postbody)) != 0) {
                            foreach (Notify::createNotify($postbody) as $key => $n) {
                                            $s = $loggedInUserId;
                                            $r = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$key))[0]['id'];
                                            if ($r != 0) {
                                                    DB::query('INSERT INTO notifications VALUES (\'\', :type, :receiver, :sender, :extra)', array(':type'=>$n["type"], ':receiver'=>$r, ':sender'=>$s, ':extra'=>$n["extra"]));
                                            }
                                    }
                            }

                    DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\', :topics)', array(':postbody'=>$postbody, ':userid'=>$profileUserId, ':topics'=>$topics));

            } else {
                    die('Incorrect user!');
            }
    }

    public static function createImgPost($postbody, $loggedInUserId, $profileUserId) {

            if (strlen($postbody) > 160) {
                    die('Incorrect length!');
            }

            $topics = self::getTopics($postbody);

            if ($loggedInUserId == $profileUserId) {

                    if (count(Notify::createNotify($postbody)) != 0) {
                            foreach (Notify::createNotify($postbody) as $key => $n) {
                                            $s = $loggedInUserId;
                                            $r = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$key))[0]['id'];
                                            if ($r != 0) {
                                                    DB::query('INSERT INTO notifications VALUES (\'\', :type, :receiver, :sender, :extra)', array(':type'=>$n["type"], ':receiver'=>$r, ':sender'=>$s, ':extra'=>$n["extra"]));
                                            }
                                    }
                            }

                    DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\', \'\')', array(':postbody'=>$postbody, ':userid'=>$profileUserId));
                    $postid = DB::query('SELECT id FROM posts WHERE user_id=:userid ORDER BY ID DESC LIMIT 1;', array(':userid'=>$loggedInUserId))[0]['id'];
                    return $postid;
            } else {
                    die('Incorrect user!');
            }
    }

    public static function likePost($postId, $likerId) {

            if (!DB::query('SELECT user_id FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$postId, ':userid'=>$likerId))) {
                    DB::query('UPDATE posts SET likes=likes+1 WHERE id=:postid', array(':postid'=>$postId));
                    DB::query('INSERT INTO post_likes VALUES (\'\', :postid, :userid)', array(':postid'=>$postId, ':userid'=>$likerId));
                    Notify::createNotify("", $postId);
            } else {
                    DB::query('UPDATE posts SET likes=likes-1 WHERE id=:postid', array(':postid'=>$postId));
                    DB::query('DELETE FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$postId, ':userid'=>$likerId));
            }

    }

    public static function getTopics($text) {

            $text = explode(" ", $text);

            $topics = "";

            foreach ($text as $word) {
                    if (substr($word, 0, 1) == "#") {
                            $topics .= substr($word, 1).",";
                    }
            }

            return $topics;
    }

    public static function link_add($text) {

            $text = explode(" ", $text);
            $newstring = "";

            foreach ($text as $word) {
                    if (substr($word, 0, 1) == "@") {
                            $newstring .= "<a href='profile.php?username=".substr($word, 1)."'>".htmlspecialchars($word)."</a> ";
                    } else if (substr($word, 0, 1) == "#") {
                            $newstring .= "<a href='topics.php?topic=".substr($word, 1)."'>".htmlspecialchars($word)."</a> ";
                    } else {
                            $newstring .= htmlspecialchars($word)." ";
                    }
            }

            return $newstring;
    }






    public static function displayPosts($userid, $username, $loggedInUserId) {
            $dbposts = DB::query('SELECT * FROM posts WHERE user_id=:userid ORDER BY id DESC', array(':userid'=>$userid));
            $username1 = DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['username'];
            $posts = "";

            foreach($dbposts as $p) {


                    if (!DB::query('SELECT post_id FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$p['id'], ':userid'=>$loggedInUserId))) {

                            $posts .= "<blockquote>   ".self::link_add($p['body'])."<br><a href='".$p['postimg']."'><img class='post' src='".$p['postimg']."'></a><br>"."
                            <form action='profile.php?username=$username&postid=".$p['id']."' method='post'>
                                    <input type='submit' name='like' value='Like'>
                                    <span>".$p['likes']." likes</span>
                            ";
                            if ($userid == $loggedInUserId) {
                                    $posts .= "<input type='submit' name='deletepost' value='delete' />";

                            }
                            $posts .= "
                            </form></blockquote><hr /></br />
                            ";

                    } else {
                            $posts .= "<blockquote> ".self::link_add($p['body'])."<br> <img class='post' src='".$p['postimg']."'>"."
                            <form action='profile.php?username=$username&postid=".$p['id']."' method='post'>
                            <input type='submit' name='unlike' value='unlike'>
                            <span>".$p['likes']." likes</span>
                            ";
                            if ($userid == $loggedInUserId) {
                                    $posts .= "<input type='submit' name='deletepost' value='x' />";
                            }
                            $posts .= "
                            </form></blockquote><hr /></br />
                            ";
                    }
            }


            return $posts;
    }




    public static function displayFosts($userid, $username, $loggedInUserId) {
            $dbposts = DB::query('SELECT posts.id, posts.body, posts.posted_at, posts.postimg, posts.likes, users.`username` FROM users, posts, followers
WHERE posts.user_id = followers.user_id
AND users.id = posts.user_id
AND follower_id = :userid
ORDER BY posts.likes DESC;', array(':userid'=>$userid));
            //$username2 = DB::query('SELECT username FROM users WHERE id=:userid', array(':username'=>$_GET['*']))[0]['username'];
            $posts = "";
            $username = "nope";

            foreach($dbposts as $p) {


                    if (!DB::query('SELECT post_id FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$p['id'], ':userid'=>$loggedInUserId))) {
                            $posts .= "<blockquote>   ".self::link_add($p['body'])."<br><a href='".$p['postimg']."'><img class='post' src='".$p['postimg']."'></a> <p>".timeCounting($time_ago).
                            $posts .= "</p>
                            </form></blockquote><hr /></br />
                            ";


                    } else {
                            $posts .= "<blockquote> ".self::link_add($p['body'])."<br> <img class='post' src='".$p['postimg']."'>";
                            $time = strtotime($p['posted_at']);

                    }
            }


            return $posts;
    }


}
?>

我真的希望代码不会被破坏。

2 个答案:

答案 0 :(得分:2)

类中的函数不是全局函数名称空间的一部分,需要以不同方式访问。有关详细信息,请参阅PHP OOP basics documentation

您想要self::timeCounting()

如果您在声明中没有static关键字,那就是$this->timeCounting()

答案 1 :(得分:1)

在第239行,它必须是:

$posts .= "<blockquote>   ".self::link_add($p['body'])."<br><a href='".$p['postimg']."'><img class='post' src='".$p['postimg']."'></a> <p>".self::timeCounting($time_ago).

而不是

$posts .= "<blockquote>   ".self::link_add($p['body'])."<br><a href='".$p['postimg']."'><img class='post' src='".$p['postimg']."'></a> <p>".timeCounting($time_ago).