Zephir Language Documentation

repository·development·Indexed 25 days ago

https://github.com/zephir-lang/zephir

Zephir is a high-level programming language that transpiles code into C to create high-performance PHP extensions. It includes the Zephir Kernel, a meta-framework built on the Zend API that simplifies memory management, array and object manipulation, and class registration. The documentation covers installation on Windows, building extensions using Visual Studio and the PHP SDK, and local development setup using Docker Compose for versions 8.0 through 8.5.

Tokens
1.3K
Snippets
4
Records
9
Agent score
86%

What's inside Zephir

  1. Overview of Zephir Kernel

    development

    Zephir Kernel is a meta-framework built on top of the Zend API designed to facilitate the creation of PHP extensions. It allows developers to write C code using a style that is familiar to PHP developers, abstracting much of the complexity of the Zend API.

    Key capabilities provided by the Zephir Kernel include:

    • Manipulation of arrays and objects.
    • Calling functions and methods within the PHP userland.
    • Automatic memory management.
    • Requiring and executing plain PHP files.
    • Simplification of common operations (e.g., string concatenation).
    • Reading superglobals and updating the active symbol table.
    • Registering classes within namespaces.
    • Throwing exceptions.
  2. Overview of Zephir

    development
    Zephir is a high-level programming language designed to simplify the creation and maintainability of PHP extensions. It works by exporting Zephir code into C code, which can then be compiled and optimized using major C compilers like gcc, clang, or vc++. The resulting functionality is exposed directly to the PHP language as an extension.
  3. Install Zephir on Windows

    development

    To install Zephir on Windows, clone or download the repository, add its directory to your system PATH, and run composer install.

    Prerequisites:

    • Visual Studio 2012 Express (activated).
    • PHP (NTS) installed and added to your PATH.
    • PHP SDK (use php-sdk-binary-tools-20110915.zip for PHP 5.6, or the Microsoft releases for PHP 7.0+).
    • PHP Developer Pack (NTS) extracted to a local directory.

    Setup Commands:

    setx path "%path%;c:\path-to-zephir"
    cd c:\path-to-zephir
    composer install
  4. Build a Zephir extension on Windows

    development

    To build a Zephir extension on Windows, you must use the appropriate Visual Studio Command Prompt and initialize the PHP SDK environment.

    1. Open the correct Command Prompt:
      • For PHP 7 or later: Open the Visual Studio 2015 Command Prompt (or run "%VS140COMNTOOLS%\VsDevCmd").
      • For PHP 5.5 or later: Open the Visual Studio 2012 Command Prompt (or run "%VS110COMNTOOLS%\VsDevCmd").
    2. Initialize the SDK: Execute %PHP_SDK%\bin\phpsdk_setvars.
    3. Build: cd to your extension directory and run zephir build.
    4. Locate Output: The resulting .dll will be in your_ext/Release/php_extname.dll (or your_ext/Release_TS/php_extname.dll for Thread Safe builds).
    # Example workflow
    "%VS140COMNTOOLS%\VsDevCmd"
    %PHP_SDK%\bin\phpsdk_setvars
    cd c:\path-to-your-extension
    zephir build
  5. Configure Windows environment variables for Zephir

    development

    Zephir requires specific environment variables to be set for the PHP SDK and Developer Pack to be recognized during the build process.

    Set the following variables using setx:

    • php_sdk: The path to your PHP SDK.
    • php_devpack: The path to your extracted PHP Developer Pack (NTS).

    Example:

    setx php_sdk "c:\path-to-php-sdk"
    setx php_devpack "c:\path-to-extracted-devpack"
  6. Set up Zephir local development environments with Docker Compose

    development
    For local development, you can use the provided docker-compose.yml to spin up multiple Zephir versions in isolated containers. The configuration defines services for Zephir versions 8.0 through 8.5. Each service maps the current directory to /srv inside the container and sets the working directory to /srv.
  7. Retrieve a class entry using Entry::get()

    development

    The Zephir\Class\Entry::get() method retrieves the underlying C-level class entry representation for a given class name. It handles several scenarios:

    1. Exclusions: Specific classes like pdostatement trigger header additions to the compilation context.
    2. Configured Entries: It checks against a predefined list of class entries loaded from config/class-entries.php.
    3. Internal PHP Classes: If the class is a built-in PHP internal class, it returns a zephir_get_internal_ce call with the appropriate string literal.
    4. External/Project Classes: For classes within the project namespace, it uses a Definition object to resolve the class entry.
    5. Unknown External Classes: For classes not recognized as internal or project-defined, it defaults to a zephir_get_internal_ce call using a lowercased, escaped version of the class name.

    Note: This method may throw a Zephir\Exception.