从一个类调用另一个类.php


call one class from another. php

我有两个类。一个用于邀请,另一个用于通知,我正在从中向设备推送通知。

我想在数据库中插入邀请时调用通知类。

我已经扩展了邀请类与通知类,但它不工作,我得到一个空的输出。

如果我运行单个邀请或单个通知类都可以正常工作。

邀请类:

 <?php
require 'database.php';
require 'notification.php';
class Invitation extends notification
{
    private $sender_id,$date,$invitee_no,$status,$invitations,$user_name,$contact_id,$contact_name;
    private $notify;
    public function setNotification($message,$user_name) {
        $this->send($message, $user_name); // calling superclass method
    }
    function Invitation($sender_id,$date,$invitee_no,$status,$user_name,$contact_id,$contact_name)
    {
        $this->sender_id = $sender_id;
        $this->date= $date;
        $this->invitee_no = $invitee_no;
        $this->status = $status;
        $this->user_name = $user_name;
        $this->contact_id = $contact_id;
        $this->contact_name = $contact_name;
        // $this -> invitations = $invitations;
    }
    function sendInvite()
    {
        $database = new Database(ContactsConstants::DBHOST, ContactsConstants::DBUSER, ContactsConstants::DBPASS, ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();
        $stmt = $dbConnection->prepare("select * from Invitation where user_name =? and sender_id = ?");
        $stmt->execute(array($this->user_name,$this->sender_id));
        $rows = $stmt->rowCount();

        if ($rows > 0) {
            $response = array("status" => -3, "message" => "Invitation exists.", "user_name" => $this->user_name);
            return $response;
        }
        $this->date = "";
        $this->invitee_no = "";
        $this->status = "0";
        $this->contact_id = 0;
        $this->contact_name = "";
        echo $this->user_name;
        echo $this->sender_id;
        $stmt = $dbConnection->prepare("insert into Invitation(sender_id,date,invitee_no,status,user_name,contact_id,contact_name) values(?,?,?,?,?,?,?)");
        $stmt->execute(array($this->sender_id, $this->date, $this->invitee_no, $this->status, $this->user_name,$this->contact_id,$this->contact_name));
        $rows = $stmt->rowCount();
        $Id = $dbConnection->lastInsertId();

        $stmt = $dbConnection->prepare("select * from Invitation where invitation_id=?");
        $stmt->execute(array($Id));
        $invitation = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($rows < 1) {
            $response = array("status" => -1, "message" => "Failed to send Invitation., unknown reason");
            return $response;
        } else {

          //  $notify = new notification();
          //  $resp = $notify->send($message, $this->user_name);
            $response = array("status" => 1, "message" => "Invitation sent.", "Invitation:" => $invitation);
            return $response;
        }
    }
通知:

    <?php
require 'database.php';
class notification
{
    private $text,$user_name;
    public function __construct()
    {
    }
    public function send($text,$userName)
    {
        $database = new Database(ContactsConstants::DBHOST, ContactsConstants::DBUSER, ContactsConstants::DBPASS, ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();
        $stmt = $dbConnection->prepare("Select device_id from Users where user_name =?");
        $stmt->execute(array($userName));
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        $token = $result["device_id"];
       // echo $token;
       // echo $text;
       // echo $userName;
        if(!empty($token)) {
            echo $token;
            $response = $this->sendPush($text, $token, "AIzaSyBGwwJaThyLm-PhvgcbdYurj-bYQQ7XmCc");
        }
    }
    public function sendPush($text, $tokens, $apiKey)
    {
        $notification = array(
            "title" => "You got an invitation.",
            "text" => $text,
            "icon" => "ic_chat_bubble_white_48dp",
            'vibrate' => 3,
            'sound' => "default"
        );
        $msg = array
        (
            'message' => $text,
            'title' => 'You got an invitation.',
            'tickerText' => 'New Message',
            'largeIcon' => 'large_icon',
            'smallIcon' => 'small_icon'
        );
        $fields = array
        (
            'to' => $tokens,
            'data' => $msg,
            'notification' => $notification
        );
        $headers = array
        (
            'Authorization: key=' . $apiKey,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        echo($result);
        return $result;
        curl_close($ch);
    }
}
?>

sendInvite脚本:

    <?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');
require 'Invitation.php';
require 'notification.php';

$jsonText = file_get_contents('php://input');
if(empty($jsonText))
{
    $response = array("status"=>-2,"message"=>"Empty request");
    die(json_encode($response));
}
$json = json_decode($jsonText);
$sender_id = $json-> sender_id;
$user_name = $json -> user_name;

echo $sender_id;
echo $user_name;
$invitation = new Invitation($sender_id,"","","",$user_name,"","");
$response = $invitation->sendInvite();
$message =  'Hi,add me to your unique contact list and you never need to update any changes anymore!';
$invitation->setNotification($message,$user_name);
echo(json_encode($response));
?>

如何积分?

编辑:

我把require改成require_once

更新代码:邀请:

  <?php
require_once 'database.php';
require_once 'notification.php';
class Invitation extends notification
{
    private $sender_id,$date,$invitee_no,$status,$invitations,$user_name,$contact_id,$contact_name;
    private $notify;
    public function setNotify($message,$user_name) {
        $this->send($message, $user_name); // calling superclass method
    }
    function Invitation($sender_id,$date,$invitee_no,$status,$user_name,$contact_id,$contact_name)
    {
        $this->sender_id = $sender_id;
        $this->date= $date;
        $this->invitee_no = $invitee_no;
        $this->status = $status;
        $this->user_name = $user_name;
        $this->contact_id = $contact_id;
        $this->contact_name = $contact_name;
        // $this -> invitations = $invitations;
    }
    function sendInvite()
    {
        $database = new Database(ContactsConstants::DBHOST, ContactsConstants::DBUSER, ContactsConstants::DBPASS, ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();
        $stmt = $dbConnection->prepare("select * from Invitation where user_name =? and sender_id = ?");
        $stmt->execute(array($this->user_name,$this->sender_id));
        $rows = $stmt->rowCount();

        if ($rows > 0) {
            $response = array("status" => -3, "message" => "Invitation exists.", "user_name" => $this->user_name);
            return $response;
        }
        $this->date = "";
        $this->invitee_no = "";
        $this->status = "0";
        $this->contact_id = 0;
        $this->contact_name = "";
        $stmt = $dbConnection->prepare("insert into Invitation(sender_id,date,invitee_no,status,user_name,contact_id,contact_name) values(?,?,?,?,?,?,?)");
        $stmt->execute(array($this->sender_id, $this->date, $this->invitee_no, $this->status, $this->user_name,$this->contact_id,$this->contact_name));
        $rows = $stmt->rowCount();
        $Id = $dbConnection->lastInsertId();
        $stmt = $dbConnection->prepare("select * from Invitation where invitation_id=?");
        $stmt->execute(array($Id));
        $invitation = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($rows < 1) {
            $response = array("status" => -1, "message" => "Failed to send Invitation., unknown reason");
            return $response;
        } else {

          //  $notify = new notification();
          //  $resp = $notify->send($message, $this->user_name);
            $response = array("status" => 1, "message" => "Invitation sent.", "Invitation:" => $invitation);
            return $response;
        }
    }
通知:

require_once 'database.php';
class notification
{
    private $text,$user_name;
    public function __construct()
    {
    }
    public function setNotification($text, $username) {
        $this->text = $text;
        $this->user_name = $username;
    }
    public function send($text, $username)
    {
        $database = new Database(ContactsConstants::DBHOST, ContactsConstants::DBUSER, ContactsConstants::DBPASS, ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();
        $stmt = $dbConnection->prepare("Select device_id from Users where user_name =?");
        $stmt->execute(array($this->username));
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        $token = $result["device_id"];
        echo $token;
        echo $this->text;
        echo $this->username;
        if(!empty($token)) {
            echo $token;
            $response = $this->sendPush($this->text, $token, "AIzaSyBGwwJaThyLm-PhvgcbdYurj-bYQQ7XmCc");
        }
    }
    public function sendPush($text, $tokens, $apiKey)
    {
        $notification = array(
            "title" => "You got an invitation.",
            "text" => $text,
            "icon" => "ic_chat_bubble_white_48dp",
            'vibrate' => 3,
            'sound' => "default"
        );
        $msg = array
        (
            'message' => $text,
            'title' => 'You got an invitation.',
            'tickerText' => 'New Message',
            'largeIcon' => 'large_icon',
            'smallIcon' => 'small_icon'
        );
        $fields = array
        (
            'to' => $tokens,
            'data' => $msg,
            'notification' => $notification
        );
        $headers = array
        (
            'Authorization: key=' . $apiKey,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        echo($result);
        return $result;
        curl_close($ch);
    }
}
?>

sendInvite:

    <?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');
require_once 'Invitation.php';
$jsonText = file_get_contents('php://input');
if(empty($jsonText))
{
    $response = array("status"=>-2,"message"=>"Empty request");
    die(json_encode($response));
}
$json = json_decode($jsonText);
$sender_id = $json-> sender_id;
$user_name = $json -> user_name;

echo $sender_id;
echo $user_name;
$invitation = new Invitation($sender_id,"","","",$user_name,"","");
$response = $invitation->sendInvite();
$message =  'Hi,add me to your unique contact list and you never need to update any changes anymore!';
$invitation->setNotification($message,$user_name);

echo(json_encode($response));
?>

现在得到如下输出:

<!DOCTYPE html>
<html lang=en>
    <meta charset=utf-8>
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
    <title>Error 404 (Not Found)!!1</title>
    <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
    <a href=//www.google.com/>
    <span id=logo aria-label=Google></span>
</a>
<p>
    <b>404.</b>
    <ins>That’s an error.</ins>
    <p>The requested URL 
        <code>/fcm/send</code> was not found on this server.
        <ins>That’s all we know.</ins>
{"status":1,"message":"Invitation sent.","Invitation:":{"invitation_id":"547","date":"","invitee_no":"","status":"0","sender_id":"50","contact_id":"0","user_name":"siddhi","contact_name":""}}

请帮. .谢谢你. .

在通知文件中像这样声明一个类变量,并在construct函数中调用database。使用$this->dbConnection代替其他地方的$dbConnection

class notification
{
    private $text,$user_name;
    public $dbConnection
    public function __construct()
    {
            $database = new Database(ContactsConstants::DBHOST, ContactsConstants::DBUSER, ContactsConstants::DBPASS, ContactsConstants::DBNAME);
            $this->dbConnection = $database->getDB();
    }
    public function send($text,$userName)
    {
        $stmt = $this->dbConnection->prepare("Select device_id from Users where user_name =?");
        $stmt->execute(array($userName));
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        $token = $result["device_id"];

然后在邀请php中删除require 'database.php';行并使用$this->dbConnection访问数据库函数

 function sendInvite()
    {
        $stmt = $this->dbConnection->prepare("select * from Invitation where user_name =? and sender_id = ?");
        $stmt->execute(array($this->user_name,$this->sender_id));
        $rows = $stmt->rowCount();

在else条件下,你不需要重新声明notification类,只需要像这样访问它。

 $resp = $this->send($message, $this->user_name);

将require更改为require_once以避免包含问题