簡體   English   中英

未捕獲的錯誤:調用成員函數

[英]Uncaught Error: Call to a member function

我正在嘗試在php文件中使用以下代碼。

global $w;
func();
function func(){
$data = getAllNumbers();
$numbers = $data["data"];
$error = $data["error"];

for($i = 0; $i < count($numbers); $i++) {
    $w->sendPresenceSubscription($numbers[$i]);
}
}

在我的whtasprot.class.php中具有如下功能

 public function sendPresenceSubscription($to)
    {
        $node = new ProtocolNode('presence', ['type' => 'subscribe', 'to' => $this->getJID($to)], null, '');
        $this->sendNode($node);
    }

但我收到如下錯誤。 我也無法修復它,也不知道為什么會出現。 如果我刪除功能代碼並直接使用它,那么它可以正常工作。

Uncaught Error: Call to a member function sendPresenceSubscription() on null in

的index.php

<?php
ini_set('memory_limit', '-1');
require_once('../src/whatsprot.class.php');
require_once('api.php');
require_once('storeStatus.php');


$starttime = time();

function onPresenceAvailable($mynumber, $from)
{
    storeStatus(explode("@", $from)[0], 1);
    echo "Online: ".$from."\n";
}

function onPresenceUnavailable($mynumber, $from)
{
    storeStatus(explode("@", $from)[0], 0);
    echo "Offline: ".$from."\n";
}


$debug = false;

$nickname = 'Test WA';
$username = '00000000';
$password = '00000000';


global $w;

$w = new WhatsProt($username, $nickname, $debug);

$w->eventManager()->bind('onPresenceAvailable', 'onPresenceAvailable');
$w->eventManager()->bind('onPresenceUnavailable', 'onPresenceUnavailable');


try {
  $w->connect();
} catch (Exception $e) {
  echo 'Connection error: ' . $e->getMessage();
  exit(0);
}

try {
  $w->loginWithPassword($password);
} catch (Exception $e) {
  echo 'Login error: ' . $e->getMessage();
  exit(0);
}
func();
function func(){
$data = getAllNumbers();
$numbers = $data["data"];
$error = $data["error"];

for($i = 0; $i < count($numbers); $i++) {
    $w->sendPresenceSubscription($numbers[$i]);
}
}

while (1) {
    try{
    $w->pollMessage();
    if(time()-$starttime > 14400) {
    break;
    }
    }
    catch (Exception $e) {
    }
 }

$w->disconnect(); 
?>

我在index.php(51):func()上遇到錯誤。 誰能建議我這有什么問題嗎? 謝謝

在函數中聲明global $w

就像是:

function funct() {
    global $w;
    //the rest of codes here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM