- URL: https://www.laruence.com/en/2012/08/31/2742.html
- Please include attribution when republishing.
Yaf is an MVC framework written as a PHP extension that I wrote about two years ago. The goal of developing Yaf was to resolve the classic tension of using a framework while suffering a performance hit.
First, I want to thank the trust of my colleagues at Baidu, and the support of the managers at the time — they allowed and had the courage to let me "try things out" and make mistakes. That's what carried Yaf through the "nobody dares to use it" phase. A large number of Baidu's new products were built on Yaf, which thoroughly validated Yaf's stability and also generally improved the execution efficiency of PHP applications.
Now, Yaf's high performance has been proven once again in Weibo's application. By migrating the framework to Yaf, along with some other optimization measures, we successfully raised the new Weibo's TPS by as much as 76%, and cut the response time by nearly half.
However, I've also noticed that quite a few people still have doubts about Yaf, and some doubts even turn into criticism. Some say "writing a framework in C? Isn't that a regression to the era of writing CGI?" So I think it's necessary for me to write an article introducing my understanding of Yaf in more detail.
First, Yaf is a framework. So what's everyone's feel about frameworks? For instance, when you use Yii or Zend Framework day to day, do you go and modify it? Once your business is built on a certain framework, do you think you'll still go modify the framework code later?
This is a precondition for why Yaf can be built as a framework: a framework is not easy to change and generally doesn't need maintenance. For example, the Yaf used at Baidu is roughly equivalent to Yaf 2.1.0. Although from 2.1.0 to the current 2.1.18 some features were added and some bugs fixed, as long as your application is built on the framework and running stably, you don't necessarily have to upgrade the framework to the latest version.
Second, when using other frameworks, if you need to debug, do you need to read the framework's code and add debug info inside the framework code to debug? What I want to say is, that situation is very, very rare. Yes, when a developer is especially familiar with a certain framework, he clearly knows the flow of these frameworks, so he might debug this way — but it's not a must.
Because Yaf is an extension, it pays particular attention to the convenience of development/debugging. It provides a plugin mechanism, provides rich debug information, and has been specially optimized for command-line debugging, giving more error reasons when an error occurs wherever possible. So, you hardly need to understand the framework's specific implementation at all to debug conveniently.
Some people are also worried about Yaf's implementation, afraid that introducing an extension would cause some other problems.
Yaf's implementation is all based on PHP's kernel API. You can think of it this way: Yaf's execution level is no different from your PHP code's execution level. It has taken full care to avoid being intrusive to PHP and obeys all the security limits. So loading Yaf inside PHP won't bring any problems (of course, unless your application has classes with the same names as those Yaf provides).
Some people worry about: what if Yaf has a bug? After all, a framework written as an extension is harder to maintain.
First, does Yaf have bugs? Of course it does — just like PHP, which also has many bugs. But as long as you don't touch them, you won't run into them. There's no such thing as "no problem during development, problem when running."
Also, for Yaf's issues, I can basically respond quickly — the large number of Yaf users today should be able to attest to that, right? If it's truly a Yaf bug, I'll definitely fix it immediately and provide a patch. Moreover, besides me, more and more people are starting to understand Yaf's implementation. I believe Yaf will have more maintainers in the future.
Also, quite a few people have asked why Yaf doesn't have an ORM. There are two considerations here:
First, Yaf is not omnipotent. It only solves the most basic problem in an application: the extra performance overhead a framework brings. And this portion of overhead, compared with your application's actual overhead, is often very small.
But, Yaf represents a spirit: pursuing simplicity, pursuing efficiency, pursuing "simple and dependable". So Yaf focuses on implementing the most core functionality and providing the most stable implementation.
Compared with an ORM, if you want to implement it conveniently, it will inevitably be complex. At the time, implementing an ORM could have introduced instability.
Second, and most importantly, PHP already provides PDO, a light wrapper around the DB. I think using PDO directly is simpler and more efficient. I don't want to provide a complex ORM wrapper that encourages people to abandon simple PDO in favor of an ORM. So, at the beginning, Yaf did not include an ORM.
Admittedly, an ORM can improve development efficiency, especially for some simple applications. So in later versions of Yaf, I'd like to consider adding an ORM — but it would absolutely be a simple ORM, similar to Yaf's built-in view engine: Yaf_View_Simple. Simple and dependable.
Of course, because Yaf is an extension, and most virtual hosting providers don't allow us to install our own extensions, this limits Yaf's applicability. On this point, I want to give special thanks to the folks at SAE/BAE. In the near future, both SAE/BAE will support Yaf, so that more individual developers can use Yaf too.
Compared with other frameworks, Yaf is still a very young thing. Although a bunch of enthusiastic folks (大门, 黑馒头, 烧鸡, and others) are working with me to enrich the documentation and provide examples, Yaf's related ecosystem is still not as rich as other frameworks.
So, for people who have never used a framework at all, the barrier to getting started with Yaf will be a bit higher. But if you've already used other frameworks, then adopting Yaf will be much simpler than you might imagine.
Finally, I'm not saying everyone needs to use a higher-performance framework. I just hope Yaf can provide everyone with one path, one more choice.
"Yaf is just the fastest framework, not the most perfect one."
I'll keep making it more and more perfect. And I also welcome you to join me in making it better.
Yaf source code on Github: https://github.com/laruence/php-yaf
Yaf release package on PECL: http://pecl.php.net/yaf
Be First to Comment