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

时间:2018-07-07 05:04:37

标签: php html mysql

我正在尝试在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()。 谁能建议我这有什么问题吗? 谢谢

1 个答案:

答案 0 :(得分:1)

在函数中声明global $w

类似:

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