Eric Lee Eric Lee - 1 month ago 8
PHP Question

how to create an alias function in php

Hi I feel like i need some alias functions in a class for real function to show that there are some functions in the class in the beginning of the class.
What I want to do is below.

class foo {

public function foo();
public function boo();
//alias functions to show there are foo and boo functions somewhere below.

public function foo(){//real function
//some code here
}
public function boo(){//real function
//some code here
}
}


I saw these kind of class before but I cannot find it anymore.

Please help.

Answer

What you saw was an Interface. Their purpose is to create sort of a map of a class so you know that any class that implements it will have at least the required methods.

interface myInterface(){
    // "alias functions"
    public function foo();
    public function boo();
}

class foo implements myInterface{
    // "real functions"
    public function foo(){}
    public function boo(){}
}

phpDoc might be closer to what you're looking for. This is a sort of comment syntax that most IDE's use to read and autocomplete methods in your classes. In Netbeans for example, your IDE will offer a method description with parameters and whatever other info you provide. Here's an example from EasyImage source code:

/**
 * Add perspective to an image
 * @param float $gradient - gradient of perspective
 * @param type $rightdown - angle
 * @return \EasyImage
 */
public function perspective($gradient = 0.85, $rightdown="top"){ ... }

Using this function in Netbeans, showing function description and parameters:

Autocomplete in Netbeans

Comments