File stream wrapper is overwritable in PHP

I find today that the default "file" stream wrapper in PHP is overwritable. You only need to call stream_wrapper_unregister and then stream_wrapper_register with your wrapper.

Imagine that you want to integrate your system A with another system B and you need to "catch" some of the files the B system includes and modify them or replace with another file.

Implementing this by patching the files of the system B comes to mind first but making the same with a custom "file" stream wrapper may be more interesting.

File: test.php5

<?php echo "I'm " . basename(__FILE__);

File: sample.php

<?php

class CustomFileStreamWrapper
{
  private $_handler;
  function stream_open($path, $mode, $options, &$opened_path) {
    stream_wrapper_restore('file');
    $this->_handler = fopen($path . '5', $mode);
    stream_wrapper_unregister('file');
    stream_wrapper_register('file', 'CustomFileStreamWrapper');
    return true;
  }
  function stream_read($count) {
    return fread($this->_handler, $count);
  }
  function stream_write($data) {
    return fwrite($this->_handler, $data);
  }
  function stream_tell() {
    return ftell($this->_handler);
  }
  function stream_eof() {
    return feof($this->_handler);
  }
  function stream_seek($offset, $whence) {
    return fseek($this->_handler, $offset, $whence);
  }
}

stream_wrapper_unregister('file');
stream_wrapper_register('file', 'CustomFileStreamWrapper');

// Includes test.php, not test.php5!
require 'test.php';

Output:

I'm test.php

Installing Debian Web-server with Apache, PHP, and MySQL on virtual machine

Installing Linux is not a complex task, you only need to follow some instructions and have basic understanding of how computer software and hardware work. Also you should be ready that documentation will be different from what you already might have seen. It means that it may be less colorful, more technical, and have different terminology. That's Ok. What you need is only to follow the doc and try to understand it - like thousands of people.

PHP micro-optimization tips

Why "micro-"? Because changing logic of your application may give you much better performance boost then applying all these tips. But they still can make your code better. You always need to output something, why do not use "echo" instead of "print"?

Small hint for PDT, use autocomplete in /* @var */type hint.

@var variable type hint is very useful, but adding it a little bit complex: usually you need to type the class name or copy/paste it from somewhere:

/* @var $controller Zend_Controller_Front */

But with Eclipse PDT templates you can simplify this and add an autocomplete for variable name and class name:

PHP IDE with autocomplete for class properties

Happy dreams of PHP developers come true. Yesterday I received a letter from Eclipse PDT bugzilla with notification that my proposition about autocomplete for properties that submitted more than a year ago is implemented in PDT 2.0 nightly builds.

Server load with PHP as module (mod_php) and PHP with FastCGI (mod_fcgid)

mod_php or PHP via mod_fcgid? Apachelounge.com shows that PHP with FastCGI is in 1.5 times better then PHP as module.

Fri, Sat, Sun - Fast CGI; Mon, Tue, Wed, Thu - PHP module; Fri - Fast CGI again.

Zend, Prado, ASP.NET. Which framework is the best?

I've been asked on the forum about my preferences in frameworks and I wrote a few thoughts about it. In short:

  • 80% of the code I have written do not operate with frameworks.
  • Language constructions, right tools and good methodology are more important then frameworks.
  • ZF style is more flexible and allows you to write faster code and its learning curve is not as high as it is for declarative frameworks (PRADO/ASP.NET).

Article about Zend Certification Exam

I've just published an article about Zend Certification. As you may know, I've passed it a year ago and since then have about 5-7 email each month with questions about it. So I've summarized my answers in article.

If you are a ZCE and wrote some words about it, just drop me a mail or leave a comment and I will add the link to your post in the article. Thanks.

Zend Certification

Zend Certification is an important step for a PHP developer. It is a very good chance to evaluate your skills, show your experience to your co-workers and potential customers. Certification benefits (briefly described at http://www.zend.com/en/services/certification/) keep up interest in everything about the certification: what it is, how to prepare for it, how to pass it, what happens after you pass it.

A quick survey: PHP portlet and portal

I wonder, is it a good idea to create a PHP implementation of the JSR 286 - Portlet 2.0 specification?

The questions I want to ask:
1. Do you know about portal/portlet technology?
2. Do you like it?
3. Is it a cool idea to create a PHP analog of javax.portlet?
4. Do you want to participate in such project?
5. Will you use it in your projects?