PHP Fatal error: Uncaught Error: Call to a member function on null
PHP Fatal error: Uncaught Error: Call to a member function on null
我是 PHP 的新手,正在尝试使用 php 从我的服务器发送 firebase 推送通知。为此,我从另一个 class 调用一个函数来从我的服务器获取 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
?>
我从另一个 class 调用了 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 class
<?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;
}
}
}
}
帮我解决这个错误,抱歉我的英语不好。
我想你需要在
换衣服
$db = new DbOperationF();
这将成为
$this->db = new DbOperationF();
它是 class 的 ans 全局变量,需要使用 $this 为其赋值
您没有使用您创建的 $db
实例,而是试图访问所附函数的 $db
。要解决此问题,您需要使用 $this->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
?>
我是 PHP 的新手,正在尝试使用 php 从我的服务器发送 firebase 推送通知。为此,我从另一个 class 调用一个函数来从我的服务器获取 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
?>
我从另一个 class 调用了 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 class
<?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;
}
}
}
}
帮我解决这个错误,抱歉我的英语不好。
我想你需要在
换衣服$db = new DbOperationF();
这将成为
$this->db = new DbOperationF();
它是 class 的 ans 全局变量,需要使用 $this 为其赋值
您没有使用您创建的 $db
实例,而是试图访问所附函数的 $db
。要解决此问题,您需要使用 $this->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
?>