Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不在对象上下文中使用$this_Php_Oop_Class - Fatal编程技术网

Php 不在对象上下文中使用$this

Php 不在对象上下文中使用$this,php,oop,class,Php,Oop,Class,我正在创建一个函数来显示博客的。所以我做了一个show blog函数,但它一直给出“不在对象上下文中使用$this”错误 Class博客{ 公共函数getLatestBlogsBig($cat=null){ $sqlString=“从jab_blog中选择blog_id”; 如果($cat!=null) $sqlString.=“WHERE blog_cat=”.$cat; $sqlString.=“按博客订购\u id描述限制5”; $blog=mysql\u查询($sqlString); 而

我正在创建一个函数来显示博客的。所以我做了一个show blog函数,但它一直给出“不在对象上下文中使用$this”错误

Class博客{
公共函数getLatestBlogsBig($cat=null){
$sqlString=“从jab_blog中选择blog_id”;
如果($cat!=null)
$sqlString.=“WHERE blog_cat=”.$cat;
$sqlString.=“按博客订购\u id描述限制5”;
$blog=mysql\u查询($sqlString);
而($id=mysql\u result($blog,“blog\u id”)){
$this->showBlog($id);//此行有错误
}
}
函数showBlog($id,$small=false){
$sqlString=“从jab_blog WHERE blog_id=“.$id.”中选择blog_id;”;
$blog=mysql\u查询($sqlString);
如果($small=true){
回声“
    ”; 而($blogItem=mysql\u fetch\u数组($blog)){ 回音“”; } 回声“
”; }否则{ 而($blogItem=mysql\u fetch\u数组($blog)){ ?>

发布日期


如何调用
getLatestBlogsBig
?如果在静态上下文中调用它(
Blog::getLatestBlogsBig()
),然后,
$this
无法解析为对象。您需要在
Blog
类的实例上调用
getLatestBlogsBig
方法。

我不明白如何使用您发布的代码到达此错误/行,因为您必须处于对象模式才能到达该行。getLatestBlogsBig()在实际运行的代码中声明静态


在静态函数中使用
$this->myFunction()
无效。请改用
self::myFunction()
。请记住myFunction()必须是静态函数

请显示正在运行的PHP代码以调用
博客
类。可能的重复是静态类和非静态类的问题。使用self::解决了问题现在一切正常感谢帮助
Class Blog{

    public function getLatestBlogsBig($cat = null){
        $sqlString = "SELECT blog_id FROM jab_blog";
        if($cat != null)
            $sqlString .= " WHERE blog_cat = " . $cat;

        $sqlString .= " ORDER BY blog_id DESC LIMIT 5";
        $blog = mysql_query($sqlString);

        while($id = mysql_result($blog,"blog_id")){
            $this->showBlog($id); //Error is on this line
        }

    }

    function showBlog($id,$small = false){
        $sqlString = "SELECT blog_id FROM jab_blog WHERE blog_id=" . $id . ";";
        $blog = mysql_query($sqlString);

        if($small = true){
            echo "<ul>";
            while($blogItem = mysql_fetch_array($blog)){
                echo '<a href="' . $_SESSION['JAB_LINK'] . "blog/" . $blogItem['blog_id'] . "/" . SimpleUrl::toAscii($blogItem['blog_title']) .'">' . 
                    $blogItem['blog_title'] . '</a></li>';
            }
            echo "</ul>";
        }else{
            while($blogItem = mysql_fetch_array($blog)){
            ?>
            <div class="post">
                <h2 class="title"><a href="<?php echo $_SESSION['JAB_LINK'] . "blog/" . $blogItem['blog_id'] . "/" . SimpleUrl::toAscii($blogItem['blog_title']);?>"><?php echo $blogItem['blog_title'];?></a></h2>
                <p class="meta"><span class="date">The date implement</span><span class="posted">Posted by <a href="#">Someone</a></span></p>
                <div style="clear: both;">&nbsp;</div>
                <div class="entry">
                    <?php echo $blogItem['blog_content'];?>
                </div>
            </div>
            <?php
            }
        }
    }
}