PHP Неустранимая ошибка: Неперехваченная ошибка: вызов функции-члена при нуле - PullRequest
0 голосов
/ 09 мая 2020

Я новичок в PHP и пытаюсь отправить уведомление firebase pu sh с моего сервера, используя php. для этого я вызываю функцию из другого класса, чтобы получить токен 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

 ?>

Я вызвал sendAdminpu sh из другого моего класса, но он выдает ошибку типа

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;

}





}     

}    

}

Помогите мне разобраться с этой ошибкой и извините за мой плохой engli sh.

Ответы [ 2 ]

0 голосов
/ 09 мая 2020

Вы не используете $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

?>
0 голосов
/ 09 мая 2020

Я думаю, вам нужно изменить

$db = new DbOperationF();

, который станет

$this->db = new DbOperationF();

Его глобальная переменная ans для этого класса, и вам нужно использовать $ this, чтобы присвоить ей любое значение

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...