Fatal error: Call to undefined function when declared [different]

Fatal error: Call to undefined function when declared [different]

嗯,经过研究我只发现 和一个 drupal 的东西。 我发现的问题与我的不同,因为他们的错误是在 if 中声明了函数,而实际情况并非如此。

让我们说清楚。我正在使用 class (classes/Post.php) 来显示一些帖子。如果我调用它,我会收到此错误:

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

这是我的Post.phpclass(第一个函数是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;
    }


}
?>

真希望密码没坏掉

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

你想要self::timeCounting().

如果声明中没有 static 关键字,则为 $this->timeCounting()

第 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).