xen:callback - not callable

Trying to implement a player count script from GitHub. Finally got the actual script in order, but it still seems to not be callable?

I am using this script: https://github.com/NoxNebula/Minecraft-Server-Status

MinecraftServerStatus.class.php is in the same folder as players.php (library/players)

library/players/players.php
PHP:
<?php

    require_once('MinecraftServerStatus.class.php');

    class Players {

        protected $minecraft_server;
        protected $ip = '158.69.22.115';

        public function __construct()
        {
            $this->minecraft_server = new MinecraftServerStatus($ip);
        }

        public function getNumplayers()
        {
            return $this->minecraft_server->Get('numplayers');
        }
    }

    echo (new Players)->getNumplayers();

?>

What I'm using in the template:
HTML:
        <div id="news">
            <div class="news-red">
                <xen:callback class="players_Players" method="getNumplayers"></xen:callback>
            </div>
        </div>

I've tried using getHtml, etc. to no avail.

The exact error I get is:

Could not execute callback players_Players::getNumplayers() - Not callable.

Any ideas?
 
Hmm, I have never used xen:callback.

A few things:

1) Change the file location / name to:

library/players/Players.php

2) In the file itself, change:

Code:
class Players

...to:

Code:
class players_Players

3) I think the function also needs to be static:

Code:
        public static function getNumplayers()
        {
            return $this->minecraft_server->Get('numplayers');
        }

4) And with a static function you need to reorganize your code to not use instanced variables:

Code:
        public static function getNumplayers()
        {
            $ip = '158.69.22.115';
            $minecraft_server = new MinecraftServerStatus($ip);
            
            return $minecraft_server->Get('numplayers');
        }

5) You might need to change your include line to this to ensure it looks to the same directory:

Code:
require_once(__DIR__ . 'MinecraftServerStatus.class.php');

6) Get rid of that standard output line:

Code:
echo (new Players)->getNumplayers();
 
Hmm, I have never used xen:callback.

A few things:

1) Change the file location / name to:

library/players/Players.php

2) In the file itself, change:

Code:
class Players

...to:

Code:
class players_Players

3) I think the function also needs to be static:

Code:
        public static function getNumplayers()
        {
            return $this->minecraft_server->Get('numplayers');
        }

4) And with a static function you need to reorganize your code to not use instanced variables:

Code:
        public static function getNumplayers()
        {
            $ip = '158.69.22.115';
            $minecraft_server = new MinecraftServerStatus($ip);
          
            return $minecraft_server->Get('numplayers');
        }

5) You might need to change your include line to this to ensure it looks to the same directory:

Code:
require_once(__DIR__ . 'MinecraftServerStatus.class.php');

6) Get rid of that standard output line:

Code:
echo (new Players)->getNumplayers();
I implemented these changes, but I get an even more puzzling error now:

ErrorException: Fatal Error: Cannot redeclare class Playercount - library/players/Players.php:5

Through trial and error, I figured out that the actual class name must differ from the file name, so I renamed it to Playercount. The structure is now library/players/Players, with Playercount being the main class in Players.php.

The new file looks like this:

PHP:
<?php

    require_once(__DIR__ . '/MinecraftServerStatus.class.php');

    class Playercount {

        public function __construct()
        {
            $this->minecraft_server = new MinecraftServerStatus($ip);
        }

        public function getNumplayers()
        {
            $ip = '158.69.22.115';
            $minecraft_server = new MinecraftServerStatus($ip);

            return $this->minecraft_server->Get('numplayers');
        }
    }

    echo (new Playercount)->getNumplayers();

?>

(for reference, I had to include the / before the include class, as XF was returning a 404 otherwise)

The template syntax looks like this:

Code:
<xen:callback class="players_Players" method="getNumplayers"></xen:callback>

Yet, that error still persists. I'm not sure how it's being duplicated, however, as this class doesn't appear anywhere else.
 
Top Bottom