未捕获错误:调用成员函数

axr492tv  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(305)

我试图在我的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

索引.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()出错。谁能告诉我这有什么问题吗?谢谢

55ooxyrt

55ooxyrt1#

声明 global $w 在func函数中。
比如:

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

相关问题