致命错误:在null上调用成员函数test()

时间:2017-03-16 19:01:15

标签: php oop

<?php
class database{
    private static $instance;
    public function __construct()
    {       
    }   
    static public function newinstance()
    {

        if(!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
    public function conn()
    {
    mysql_connect('localhost','root','') or die("database connection error".mysql_error());
    mysql_select_db('user') or die("cannot select db".mysql_error());

    }
    public function test()
    {
        echo "Public function test";
    }

}
$db=database::newinstance()->conn();
$db->test();

?>

当我发送这个php文件时,我收到了这个错误:致命错误:在null上调用成员函数test(),可能是什么问题?谢谢!

3 个答案:

答案 0 :(得分:2)

ContentPresenter没有返回任何内容,因此<Style TargetType="ContentPresenter"> <Setter Property="Margin" Value="-5"/> </Style> 永远不会被设置为任何内容。也许你打算这样做?:

conn()

答案 1 :(得分:0)

PivotTable方法不返回任何内容,您已将其分配给Dim PvtTbl As PivotTable ' set the Pivot Table into a variable Set PvtTbl = ActiveSheet.PivotTables("Tabela dinâmica1") Dim i As Long For i = 7 To 11 With PvtTbl With .PivotFields("MODEL2") .ClearAllFilters .PivotItems("CV").Visible = False .PivotItems("(blank)").Visible = False End With cel4 = "AV" & i cel3 = "AX" & i ' === Not debugging your formula part === Worksheets("DDTZ C").Range("AZ" & i).Formula = "=Iferror(IF(" & cel4 & "="""","""",VLookup(" & cel3 & ", AT:AV, 3, False)),"""")" With .PivotFields("MODEL2") .ClearAllFilters .PivotItems("IJ").Visible = False .PivotItems("(blank)").Visible = False End With ' === Not debugging your formula part === Worksheets("DDTZ C").Range("BA" & i).Value = "=Iferror(IF(" & cel4 & "="""","""",VLookup(" & cel3 & ", AT:AV, 3, False)),"""")" End With Next i 。试试:

conn()

或者您可以在$db方法中返回$db = database::newinstance(); $db->conn(); $db->test(); 并使用您的语法:

$this

答案 2 :(得分:0)

试试这个......

<?php
    class database{
        private static $instance;
        public function __construct()
        {       
        }   
        static public function newinstance()
        {

            if(!self::$instance)
            {
                self::$instance = new self();
            }
            return self::$instance;
        }
        public function conn()
        {
        mysql_connect('localhost','root','') or die("database connection error".mysql_error());
        mysql_select_db('user') or die("cannot select db".mysql_error());

        }
        public function test()
        {
            echo "Public function test";
        }

    }
    $db=database::newinstance();
    $db->conn();
    $db->test();

    ?>