【问题标题】:PHP Fatal error: Uncaught Error: Call to a member function on nullPHP 致命错误:未捕获的错误:在 null 上调用成员函数
【发布时间】:2020-08-24 18:25:36
【问题描述】:

我对 PHP 非常陌生,并尝试使用 php 从我的服务器发送 firebase 推送通知。为此,我从另一个类调用一个函数来从我的服务器获取 firebase 令牌并发送 firebase 通知

<?php 


class sendAdminpush {
  private $db;
   function __construct()
{
   //importing required files 
require_once 'DbOperationF.php';
require_once 'Firebase.php';
require_once 'push.php';  
$db = new DbOperationF();
}


 public function sendNotificationtoAdmin($title, $message,$usertype){


      $notId = rand(10,1000);
     $sound = "notification";
     $image= "ic_waterlogo";

      //creating a new push
    $push = null; 
    $push = new Push(
                $title,
                $message,
                $image,
                $notId,
                $sound
            );
        //getting the push from push object
    $mPushNotification = $push->getPush(); 

    //getting the token from database object 
    $devicetoken = $db->getAllTokens($usertype);

    //creating firebase class object 
    $firebase = new Firebase(); 
    //echo "tok:".$devicetoken."and p".$mPushNotification;
    //sending push notification and displaying result 
    echo $firebase->send($devicetoken, $mPushNotification);

 }

  }

   //class end

 ?>

我从我的另一个班级调用了 sendAdminpush 但它给出了一个错误,如

PHP Fatal error:  Uncaught Error: Call to a member function getAllTokens() on null in /home/ihdi/public_html/tupo.in/tupo/includes/Firebase/sendAdminpush.php:36

还有我的 DbOperationF 类

  <?php

  class DbOperationF
   {
//Database connection link
private $conn;

//Class constructor
function __construct()
{

    // //Getting the DbConnect.php file
       require_once dirname(__FILE__) . '/../DbConnect.php';


    //require_once '../DbConnect.php';

    // //Creating a DbConnect object to connect to the database
     $db = new DbConnect();
    // //Initializing our connection link of this class
    // //by calling the method connect of DbConnect class
   $this->conn = $db->connect();
}




//getting all tokens to send push to all devices
public function getAllTokens($usertype){
        echo "ui:".$token;
    $stmt = $this->conn->prepare("SELECT token from fcm_token WHERE user_type=?");
    $stmt->bind_param("s", $usertype);
    $stmt->execute();

     //$stmt->bind_result($token);
     $result = $stmt->get_result();

    $tokens = array(); 

    while($token = $result->fetch_assoc()){

        array_push($tokens, $token['token']);

    }
    return $tokens;

}





}     

}    

}

帮我解决这个错误,对不起我的英语不好。

【问题讨论】:

  • 这是因为你的MYSQL查询结果是一个空的结果集。只需调试您请求用户类型的 fcm_token 表值的结果......

标签: php fatal-error dbconnection


【解决方案1】:

您没有使用您创建的$db 实例,而是尝试访问封闭函数的$db。要解决此问题,您需要使用$this-&gt;db 访问全局$db。一个例子:

<?php

class sendAdminpush
{
    private $db;
    function __construct()
    {
        //importing required files
        require_once 'DbOperationF.php';
        require_once 'Firebase.php';
        require_once 'push.php';
        $this->db = new DbOperationF();
    }

    public function sendNotificationtoAdmin($title, $message, $usertype)
    {

        $notId = rand(10, 1000);
        $sound = "notification";
        $image = "ic_waterlogo";

        //creating a new push
        $push = null;
        $push = new Push($title, $message, $image, $notId, $sound);
        //getting the push from push object
        $mPushNotification = $push->getPush();

        //getting the token from database object
        $devicetoken = $this->db->getAllTokens($usertype);

        //creating firebase class object
        $firebase = new Firebase();
        //echo "tok:".$devicetoken."and p".$mPushNotification;
        //sending push notification and displaying result
        echo $firebase->send($devicetoken, $mPushNotification);

    }

}

//class end

?>

【讨论】:

  • 请不要推荐使用global
  • 当然,我不应该推荐。我刚才提到的是有哪些替代品。
【解决方案2】:

我认为你需要改变

$db = new DbOperationF();

会变成什么样子

$this->db = new DbOperationF();

该类的 ans 全局变量,需要使用 $this 为其分配任何值

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 2020-10-04
    • 2016-04-20
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2021-12-18
    相关资源
    最近更新 更多