php - Uncaught Error : Call to a member function

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

最佳答案

在 func 函数内声明 global $w

类似于:

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

关于php - Uncaught Error : Call to a member function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51220082/

相关文章:

html - Flex/Grid 布局不适用于按钮或字段集元素

php - 使用 php 和 mysql 从表单插入数据库表时收到错误

mysql查询性能低

mysql - Mysql中如何拆分列?

php - 加盐密码?

php 可编写脚本的网络浏览器

php - 如何从db中获取记录?

javascript - 动态创建多个输入字段。将它们提交到一个数组中

php - 使用 PayumBundle 注册自定义网关

python - 带有 jinja 的条件元素类,如果列表项包含某个项目,我希望 div 获得一个类