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

Php 未捕获错误:调用成员函数,php,html,mysql,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]); } } 但我得到的错误如下。我也无法修复它

我试图在我的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]);
}
}
但我得到的错误如下。我也无法修复它,也不知道它为什么会来。如果我删除函数代码并直接使用它,它就可以正常工作

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()上遇到错误。 谁能告诉我这有什么问题吗?
谢谢

在func函数中声明
global$w

比如:

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

$w
在哪里定义/实例化?@mxcoder让我发布完整代码。@mxcoder我发布了完整代码。请检查一下
function funct() {
    global $w;
    //the rest of codes here
}