如何在课堂上找到方法

时间:2017-09-29 09:31:33

标签: php function

我需要检查方法收集器。我运行一个函数收集器();它运行。但是我没有看到这个功能。

#include <iostream>
#include <curses.h>
using namespace std;

bool gameOver;
bool pressed;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

void Setup() {
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}

void Draw() {
    system("clear");
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "#";
            if (i == y && j == x)
                cout << "O";
            else if (i == fruitY && j == fruitX)
                cout << "F";
            else
                cout << " ";

            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;
}

void Input() {
    switch (getch()) {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        case 'x':
            gameOver = true;
            break;
    }
}

void Logic() {
    switch (dir) {
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UP:
            y--;
            break;
        case DOWN:
            y++;
            break;
        default:
            break;
    }
}

int main() {
    Setup();
    while(!gameOver) {
        Draw();
        Input();
        Logic();
    }
    return 0;
}

这是我的班级

2 个答案:

答案 0 :(得分:0)

When a method that doesn't exist is called on an object, PHP will fall back to the __call method, if it's defined in the class. The definition of this function must be:

public mixed __call ( string $name , array $arguments );

$name will be the name of the function that was called (in this case, collector), and $arguments will be an array of the arguments provided to it, in order. This is often used to provide "dynamic" method definitions, based on the current properties of a given instance.

This is documented here: http://php.net/manual/en/language.oop5.overloading.php#object.call

答案 1 :(得分:0)

我想您正在说如果将其命名为collector

,该如何调用该函数
public function __call(string $name, array $args)
{
    if($name === 'collector'){
        echo $name;
    }
}

您只能在此获取函数名称并为给定名称执行特定任务,但是如果要在另一个类中调用该函数,则必须实例化该类并调用该方法,或者您可以使用任何其他方法比如call_user_method_array()......