Table of Contents
Phalcon
Zephir?
Are there any code samples? What about Phalcon’s performance?
Trying Zephir today
Conclusion
Home Backend Development PHP Tutorial Zephir - Build PHP Extensions Without Knowing C

Zephir - Build PHP Extensions Without Knowing C

Feb 23, 2025 am 08:50 AM

Zephir - Build PHP Extensions Without Knowing C

Zephir - Build PHP Extensions Without Knowing C

Due to PHP being written in C, for a long time now the only way to extend it was to either rely on the current generation of enthusiastic greybeards, or take up C. For many, the latter wasn’t an option. We, the high level developers of today, are far too lazy to take up such a wonderfully precise, demanding, low level, OCD-centric language. Most of us who began our programming careers in high level languages – especially high level languages that are, to be honest, hacks on hacks on hacks like Javascript and PHP – are quite frankly completely disinterested in learning all the magic C asks of you. This is not to say we’re worse developers than the ones dealing with low levels exclusively – it’s just two different worlds – we simply love the instant gratification of web development. It would take me just as long to waddle through an intermediate C program as it would an experienced low level developer to develop a pretty cross-browser compatible JS radial context menu. So how, then, is a PHP developer supposed to enrich PHP itself with much needed functionality?

Phalcon

A while back, I mentioned the Phalcon framework in an article. Phalcon is a framework that offers more than most of the current frameworks, but is wrapped up as a PHP extension you install like IMAP, iconv, mcrypt or any other. I won’t go into details right now, the links above provide plenty information, but what’s important here is the magnitude of the milestone the Phalcon team achieved.

Phalcon is a wonderful hybrid created by wonderful hybrids

Phalcon is a wonderful hybrid created by wonderful hybrids – people who are excellent at both PHP and C. Phalcon is the golden middle ground, a PHP framework that’s as low level as it gets (immeasurable performance gains when compared to other frameworks) but its API still high level enough for any experienced PHP developer to use just like in any other framework.

So what is Zephir and what does it have to do with Phalcon? As it turns out, the developers of Phalcon realized pull requests to a C project like Phalcon are coming in far too slowly when compared to the speed of growth in Phalcon’s popularity – in no small part due to the rarity of high quality open-source oriented C developers. Fearing a bottleneck, they undertook another adventure that’s poised to redefine the world of PHP development as we know it. They decided to build Zephir.

Zephir?

If Phalcon is the golden middle ground between C and PHP, Zephir is platinum. Zephir will be (note I’m saying will be because it’s still in very early alpha stages) the only language that lets high level developers develop low level PHP extensions.

Zephir lets high level developers make low level PHP extensions

Zephir will be a static/dynamic typed language that compiles into C. It will have automatic memory management and will enforce strict and explicit over flexible and implicit behavior. The aim is to hide unnecessary C complexity from developers, make the code more debuggable, and produce object oriented libraries that can be used directly in PHP just like any other installable extension. The Phalcon team is currently rewriting the entire Phalcon framework in Zephir, gearing up for version 2.0, which will allow the huge pool of PHP devs using Phalcon to extend it without knowing C.

In addition to being used to develop and maintain Phalcon, Zephir is already in use elsewhere. The Apigility project by the Zend team will be integrating Zephir in order to allow users to improve performance of the APIs built with Apigility. What’s more, knowing Zephir will have the additional benefit of allowing you to write your entire web app in it, compile it to C, and install it into your client’s servers without exposing your code to anyone. Zephir is taking code protection, PHP app performance and development of PHP extensions to entirely new levels.

Zephir lets you truly close the source of your PHP application, protecting your intellectual property

It’s important to note that Zephir is not a replacement for any current web language. It’s not a replacement for C either. It’s a gateway between C and PHP, allowing you to write high performance PHP extensions and closed-source PHP apps without the headaches and timewastage of a C development cycle. It will neither replicate all of PHP’s functionality, nor all of C’s functionality – it’s not intended as an alternative to either. In fact, not every app should be rewritten with Zephir, as it might turn out to be nothing but a waste of time – the apps that should be rewritten are, for example, those you care about close-sourcing, and those that are computationally too intensive for the PHP interpreter. Rewriting something like WordPress in Zephir, for example, would be a fool’s errand because WordPress is a huge yarn of low quality spaghetti code, and more would be gained by rewriting it to proper, modern object oriented PHP5.5.

Are there any code samples? What about Phalcon’s performance?

There are some examples here and in the documentation. Essentially, it’s quite similar to the languages you already know – Javascript, PHP, maybe even C, making Zephir an ideal candidate to pick up and try out quickly.

Now, many wonder about the performance impact. If Phalcon is so fast due to it being written in C, won’t rewriting it in a higher level language slow it down? Well, yes and no. While it won’t allow the developers to dive into the nitty gritty of manual memory space allocation and whatnot, it will allow them to work faster, which will in turn let them iterate more rapidly and improve the design rather than improving only existing implementation. Naturally, some parts will remain written in C simply because there’s just no way to rewrite them and keep their quality.

Some initial benchmarks of Phalcon’s current implementation versus its Zephir version can be found here.

Trying Zephir today

To try Zephir for yourself (or, if you’re an amazing C developer and you’d like to help) you can check out the Github repository. From there, you can also download, build and install the language – just follow the README instructions. Preliminary documentation can be found on the Zephir project home page and their blog is available as well. For those using TextMate or Sublime Text, a syntax highlighter can be found here.

Give Phalcon and Zephir a go, and let us know in the comments below how you like them. If you try out Zephir and develop an extension with it, feel free to drop me a line about turning it into a tutorial.

Conclusion

If you’ve always wanted to improve PHP on your own, but never took up C properly to do so, Zephir is for you. If you want to protect your code from prying eyes and piracy, just improve performance of your apps, or extend PHP with functionality that can’t be effectively replicated through PHP itself, Zephir is for you.

I hope you enojyed this brief overview of this promising new project, and that you’re as excited as we are about the benefits it could bring.

The above is the detailed content of Zephir - Build PHP Extensions Without Knowing C. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles