php函数在定义时未定义

时间:2013-11-19 15:45:32

标签: php

我对我所假设的范围有一些问题,但我不能为我的生活弄清楚那个问题是什么......

每当调用它们时,我会继续为以下三个函数获取函数未定义错误; addUser,removeUser,updatePlayer。任何人都知道什么是错的?下面的完整代码(它是一个pocketmine插件):

<?php

/*
__PocketMine Plugin__
name=X
description=X
version=0.0.1
author=X
class=X
apiversion=10
*/

class X implements Plugin {
    private $api;

    public $continents = array();
    public $allContinents = array("NA" => 0, "SA" => 0, "AF" => 0, "AS" => 0, "OC" => 0, "EU" => 0);

    public function __construct(ServerAPI $api, $server = false){
        $this->api = $api;
    }

    public function init(){
        //Initialize the on player join event handler
        $this->api->addHandler("player.spawn", array($this, "onPlayerJoin"));
        $this->api->addHandler("player.quit", array($this, "onPlayerQuit"));

        //Setup Config
        $this->path = $this->api->plugin->configPath($this);
        $this->msgs = new Config($this->path . "config.yml", CONFIG_YAML, array("AuthToken" => "", "Delay" => "5"));
        $this->msgs = $this->api->plugin->readYAML($this->path . "config.yml");

    }




    // <--- updatePlayer --->
    public function updatePlayer($user) {


    }

    // <--- Country Handler --->
    public function addUser($user, $continents, $allContinents) {
        $ip = $user->ip;
        $username = $user->username;

        $gi = geoip_open($this->path ."GeoIP.dat",GEOIP_STANDARD);
        $continent = geoip_continent_code_by_name($gi, $ip);

        array_push($continents, $username);
        $continents[$username] = $continent;

        $allContinents[$continent] += 1;

                return $continents;
                return $allContinents;
    }

    public function removeUser($user, $continents, $allContinents) {
            $username = $user->username;
            $continent = $continents[$username];
            unset($continents[$username]);

            $allContinents[$continent] -= 1;

            return $continents;
            return $allContinents;
    }


    // <--- Rquest Ad --->
    public function requestAd() {

    }

    /*public function send() {
        while(true) {
            sleep(240);
        }
    }
    }*/

    //On player join event handler
    public function onPlayerJoin($user){

        $username = $user->username;
        addUser($user);
        updatePlayer($user);

        //Check if plugin is setup
        if ($this->api->ban->isOp($username) && $this->msgs["AuthToken"] == "") {
            $this->api->chat->sendTo(false, "Please visit X to setup X.", $username);
        }
    }

    public function onPlayerQuit($user){
        removeUser($user);
    }
    public function __destruct(){
    }
}
?>

2 个答案:

答案 0 :(得分:5)

如果您是从同一个班级或扩展班级调用它们,那么您需要使用$this

public function onPlayerJoin($user){

    $username = $user->username;
    $this->addUser($user);
    $this->updatePlayer($user);
}

除非它是静态函数,否则您将使用self::

答案 1 :(得分:0)

您需要实例化类X的实例,然后将函数作为该对象的成员调用。例如:

$myX = new X();
$myX->addUser();

干杯