语法错误,意外的'$ db'(T_VARIABLE),期望的函数(T_FUNCTION)

时间:2018-10-15 14:13:40

标签: php class oop

我试图简单地调用类函数,并且此错误不断困扰着我

  

语法错误,意外的'$ db'(T_VARIABLE),期望的函数(T_FUNCTION)

database.php

<?php
    class db
    {
        public static Somefunction($pr1, $pr2)
        {
            // SOME CODE HERE
        }
    }
?>

dtCall.php

<?php
    require_once "database.php";
    class database
    {
        $db = new db();

        public function myFun()
        {
            $result = $db->Somefunction($pr1, $pr2);
        }
    }
?>

2 个答案:

答案 0 :(得分:2)

这可能会对您有所帮助:

#include<stdio.h>
#include<math.h>
int main()
{
    int N;
    scanf("%d", &N);
    int numprime;
    numprime = 0;
    int P=1;
    for (P; P<N; P++)
    {
        if (P%2==0)
            continue;
        int e = sqrt(P);
        for (int j=3;j<=e;j+=2)
        {
            if (P%j!=0)
            {
                numprime = numprime + 1;
            }
            else
            {
                continue;
            }
        }
    }
    printf("%d", numprime);
}

答案 1 :(得分:-1)

之所以发生这种情况,是因为您需要指定$db是公共的,受保护的还是私有的。只需在变量声明前面添加publicprivate或“ protected”:

<?php
    require_once "database.php";
    class database
    {
        public $db = new db();   #<== Add to this line 

        public function myFun()
        {
            $result = $db->Somefunction($pr1, $pr2);
        }
    }
?>

如果您不确定该使用哪一种,请查看这篇文章:What is the difference between public, private, and protected?

解决此问题后,您将收到一个新错误。那是因为类常量必须是常量。请查看PHP文档以获取更多信息:http://php.net/manual/en/language.oop5.constants.php