Previous topic

< Exceptions

Next topic

Closures >

This Page

Calling FunctionsΒΆ

PHP has a rich library of functions that you can use within your extensions. To call a PHP function you simply use it as normal within your Zephir code:

namespace MyLibrary;

class Encoder
{

    public function encode(var text)
    {
        if strlen(text) != 0 {
            return base64_encode(text);
        }
        return false;
    }
}

You can also call functions that are expected to exist in the PHP userland but aren’t built in to PHP:

namespace MyLibrary;

class Encoder
{

    public function encode(var text)
    {
        if strlen(text) != 0 {
            if function_exists("my_custom_encoder") {
                return my_custom_encoder(text);
            } else {
                return base64_encode(text);
            }
        }
        return false;
    }
}

Note that all PHP functions only receive and return dynamic variables. If you pass a static typed variable as a parameter, a temporary dynamic variable will automatically be used as a bridge in order to call the function:

namespace MyLibrary;

class Encoder
{

    public function encode(string text)
    {
        if strlen(text) != 0 {
            // an implicit dynamic variable is created to
            // pass the static typed 'text' as parameter
            return base64_encode(text);
        }
        return false;
    }
}

Similarly, functions return dynamic values which cannot be directly assigned to static variables without the appropriate cast:

namespace MyLibrary;

class Encoder
{

    public function encode(string text)
    {
        string encoded = "";

        if strlen(text) != 0 {
            let encoded = (string) base64_encode(text);
            return "(" . encoded . ")";
        }
        return false;
    }
}

Zephir provides a way for you to call functions dynamically, such as:

namespace MyLibrary;

class Encoder
{

    public function encode(var callback, string text)
    {
        return {callback}(text);
    }
}