• Namespace Operator Nov 29, 2005

    Posted by Mike Naberezny in PHP

    If you have been trying to follow the countless threads on the php-internals mailing list regarding a new operator for namespaces, my script below will definitely save you time.

    // generate a new namespace operator suggestion
    function namespaceOperator() {
        return chr(rand(33,47)) . chr(rand(33,47)) ;
    }
    
    // predict next ten suggestions to php-internals
    for ($i=0; $i<10; $i++) {
        echo namespaceOperator() . "\n";
    }
    

    Update: http://news.php.net/php.zend-engine.cvs/5894

  • Zend PHP Conference Oct 13, 2005

    Posted by Mike Naberezny in PHP

    There is definitely some excitement in the air now that the Zend PHP Conference is less than a week away (Oct. 18th – 21st). I’m happy to say that I’ll be attending the conference, along with the rest of the US-based Zend staff, and I am greatly looking forward to it. There are a host of interesting talks promoting PHP use in the enterprise, from both business leaders and tech luminaries.

    On the business front, Zend has somehow managed to corral an impressive lineup of industry leaders, all anxious to pump PHP for business. Among them are Marc Andreessen of Netscape fame, Ken Jacobs from Oracle, Rod A. Smith from IBM, and Adam Bosworth from Google.

    For developers, this promises to have one of the best tech lineups of any recent conference, with many PHP core team members presenting. Among the talks I hope to attend are Unicode support in PHP 6, PDO: PHP Data Objects, Service Data Objects for PHP, and Oracle and PHP.

    Before we start bouncing between all of these great talks, Matthew Weier O’Phinney and I will be giving a tutorial session on Setting up PHP.

    If you haven’t signed up for the conference yet, there’s still time to register!

    Update: Slides (PDF) from the session are now available.

  • Zend Studio Remote Debugging Sep 11, 2005

    Posted by Mike Naberezny in PHP

    Remote debugging is one of the most powerful features of Zend Studio, whether used with a webserver on your local machine for development or to profile and troubleshoot a production server.

    To use the remote debugging and profiling features of Studio, the webserver must have the Zend Studio Server component installed. This is a special PHP extension that interfaces with the Zend Engine to provide powerful debugging and inspection capabilities, and provides the socket-based communication back to the Studio client for remote control.

    While the debugger is so powerful that it might seem almost magical at first, the mechanics of its operation are very straightforward. When a URL is selected for debug in the Studio client, it activates your web browser and directs it to load the specified URL, with a special set of GET request variables tacked on the end that cause the Studio Server extension to spring into action. When the page loads, the extension opens a connection back to the Studio client on the debug port. Once the two-way communciation is established, the browser hangs in page-load limbo while the debugging commences.

    Studio knows how to open your browser by the “Browser Activation Command”, and changing this can be useful. This option can be found on the “Preferences” dialog, accessible from “Tools” on the menu bar. On Windows, the Studio will be preconfigured to use Internet Explorer, so its default will be “\Program Files\Internet Explorer\iexplore.exe“. If you’d like to have it open Firefox instead, change the activation command to “\Program Files\Mozilla Firefox\firefox.exe“. The browser activation command is also used when the “Show In Browser” option is invoked from the context menu by right-clicking on the Debug Output pane.

    On UNIX-like operating systems, the default browser activation command will be “mozilla“, a shell script that loads the browser. This can sometimes be a headache for users who do not have this script or the script cannot be found because it is not in the environment PATH. If Studio is having trouble activating your browser, drop to a shell and troubleshoot by trying to enter the same command manually. Once you are able to run it from any arbitrary directory on the shell, Studio should have no trouble activating it.

    There are two small drawbacks that become apparent when using the debugger or profiler on a regular basis. First, it takes a noticeable amount of time to open the browser window with your debug URL, and you must wait on your browser during this time for each page that you debug. Second, depending on your browser, Studio may open a new browser window each time the debugger or profiler is invoked. It’s very easy to find yourself with a pile of useless browser windows open, especially after repeatedly running the profiler. However, this isn’t a drawback in Studio itself, it’s just the browser’s behavior.

    On both Windows and UNIX-like systems, there is a handy trick that eliminates both the startup latency and the extraneous browser windows. You can set your browser activation command to run cURL instead of your browser. On Windows, download the cURL binaries and place them somewhere like “\curl“. Next, change the activation command to “\curl\curl.exe“. Now, Studio will use cURL whenever the debugger or profiler is invoked. Compared to launching the brower, cURL will appear instantaneous, and it will also not open any additional windows. You can now profile as many times as you’d like without ever leaving Studio or closing extra windows. The only downside of this trick is that the “Show In Browser” option will no longer launch the browser, however you can easily change the browser activation command back at any time if you need this capability.

  • Upgrading Zend Studio’s Internal Debugger Jul 13, 2005

    Posted by Mike Naberezny in PHP

    Zend Studio for Windows ships with an “internal” debugger for running your scripts in Studio without a webserver. Studio 4.0.2 ships with PHP 5.0.3. Recently, I encountered a bug in 5.0.3 that was corrected in 5.0.4 and I wanted to upgrade the internal debugger myself rather than wait for the next Studio release. Here’s how to do it.

    First, download the ZIP package of Windows binaries from PHP downloads. Extract the files php-cgi.exe and php5ts.dll to a temporary folder. Rename the file php-cgi.exe to php.exe. This is necessary because Studio needs the CGI version of PHP, and the ZIP package’s php.exe is the CLI version.

    Next, find Zend Studio’s internal PHP5 interpreter. This is the default installation path: C:\Program Files\Zend\ZendStudioClient-4.0.2\bin\php5

    Notice two files in this directory: php.exe and php5ts.dll. Backup these two files and then remove them from the directory. Finally, replace these with the versions from your temporary folder, and the upgrade is complete. You can write a simple script with phpinfo(); inside Studio to verify the upgrade.

  • PHP and VISA Jul 4, 2005

    Posted by Mike Naberezny in PHP

    For a short time now, I have been interested in hooking up PHP with VISA (Virtual Instrument Software Architecture) to allow Windows users of PHP to control any I/O that is supported by the VISA standard. This includes serial, GPIB, VXI, PXI, USB, and other interfaces. I think that PHP would be a very good language for test automation in general because of its easy syntax and large number of convenience functions for string handling and other tasks common to instrument control.

    I thought that I had a made a significant discovery when I learned about VISA-COM, a set of COM interfaces to VISA. Accessing VISA through COM in PHP would be very convenient. I spent some time looking at NI-VISA‘s implementation of VISA-COM and also talking to the folks over at National Instruments. Unfortunately, the VISA-COM interfaces are not based on IDispatch so they cannot be used late-bound from scripting environments. Code written in VBScript to use VISA-COM will not work, however that same code will indeed work in Visual Basic 6.0. PHP 5 will also not be able to make use of VISA-COM.

    With VISA-COM out of the picture, that leaves two options. The FFI extension in PECL replaces the old W32API extension for PHP 4 and provides a “Foreign Function Interface” for PHP5 to access external libraries. FFI development seems slow and its present state may not have all of the functionality required to control the VISA DLL. However, it’s definitely worth some experimentation. The other option, which is perhaps the most desirable, is to create PHP bindings for VISA as a PHP extension.

  • Zend Certified Engineer May 31, 2005

    Posted by Mike Naberezny in PHP

    ZCEWell, I finally found some time to get down to the testing center to take the Zend PHP Certification, and I passed.

    I am now a Zend Certified Engineer.

  • Controlling iTunes from PHP 5 May 30, 2005

    Posted by Mike Naberezny in PHP

    If you are running PHP 5 on Windows, there is support for COM interfaces built right into the PHP core. The COM support in PHP 5 allows you to automate all kinds of software that expose COM interfaces, such as Microsoft Office. Everyone’s favorite music player, iTunes, also exposes a very nice set of COM interfaces that are easily controlled from PHP 5.

    To get started, instantiate a new COM client to iTunes. If iTunes is not currently running, it will be opened automatically.

    // Instantiate a new COM client to iTunes
    $iTunes = new COM('iTunes.Application');
    

    This creates an object $iTunes which is a client to the IiTunes interface, the top-level interface to the iTunes application. From here you can directly access a number of useful methods and properties for controlling iTunes, or you can access the other interfaces that it provides.

    // Easy Controls
    $iTunes->Play();
    $iTunes->Pause();
    $iTunes->Stop();
    $iTunes->PreviousTrack();
    $iTunes->NextTrack();
    $iTunes->SoundVolume = 50;
    

    To monitor what’s currently playing in iTunes, you can use the CurrentTrack property of IiTunes to return an IITrack.

    // What's playing now?
    $currentTrack = $iTunes->CurrentTrack;
    $trackName = $currentTrack->Name;
    $trackNumber = $currentTrack->TrackNumber;
    $albumName = $currentTrack->Album;
    $artistName = $currentTrack->Artist;
    

    While the capitalization of the methods and properties isn’t the normal way for PHP objects, I’d recommend doing it this way to stay consistent with the naming conventions of the iTunes COM classes. They are actually not case sensitive.

    These examples only scratch the surface of what you can do with the iTunes interfaces. To learn more about controlling iTunes with COM, you can get the complete iTunes SDK from Apple which includes documentation in CHM (Windows Help) format.

    Note: While these examples look like they should work on PHP 4, they consistently caused my PHP 4.3.10 interpreter to crash. COM support was completely rewritten for PHP 5 and all of the examples here were tested under the CLI version of PHP 5.0.3 without any problems. Please note that these examples will most likely not run under a webserver.

  • Python Module “xmlrpclib” Apr 27, 2005

    Posted by Mike Naberezny in Python

    Whether you are a fan of Python or not, Python’s bundled module xmlrpclib from PythonWare is extremely useful for testing an XML-RPC server when used from the Python interactive shell. Since xmlrpclib is bundled with the standard Python 2.3 distribution, no additional installation is required. Python itself comes preinstalled on most Linux distributions and Mac OS X. Start the Python shell, and enter these commands:

    import xmlrpclib
    server = xmlrpclib.Server('http://your/server/url')
    

    Now, you can interactively test the methods of your XML-RPC server from the Python shell. As an example, most XML-RPC server libraries include support for the system.listMethods() and system.methodSignature() methods. To list all of the available methods on the remote server from Python, simply enter:

    server.system.listMethods()
    

    The methods will be returned as a Python list and printed to stdout. Calling XML-RPC methods really couldn’t be any simpler. To get the signature of a method, enter:

    server.system.methodSignature('method_name')
    

    The conversion of data types between Python and XML-RPC is automatically performed by the Server object. You can test any of your remote server’s methods as shown above. The Server object also provides a verbose mode that will display the raw XML-RPC transactions with the server for troubleshooting. To enable verbose mode, instantiate the Server like this:

    server = xmlrpclib.Server('http://your/server/url', verbose=True)
    

    Interactively testing your server API like this can greatly reduce your debugging time. It is also very handy for getting to know your way around the APIs of other XML-RPC servers, such as WordPress or Flickr.

  • PEAR XML_RPC Bug #4231 Apr 27, 2005

    Posted by Mike Naberezny in PHP

    Today I submitted Bug #4231 to PEAR. An expanded version of my bug report can be found on this website.

    One thing that using Python’s bundled xmlrpclib on the Python interactive shell has taught me is that it’s very easy to make RPC method calls with bad parameters. Using a server built on PEAR’s XML_RPC_Server, calling a method with a bad parameter count will cause PHP undefined variable notices to be prepended to the return payload if notices have not been disabled.

    This is a security issue because the notices reveal the script path to the client. Also, this makes troubleshooting RPC calls somewhat difficult because although the server’s XML response remains intact, most clients I have tried will report a parse error when the PHP notices are encountered. This is true of the clients in PEAR’s XML_RPC, Python’s xmlrpclib, and CPAN’s XMLRPC::Lite for Perl.

    Update 9-May-2005:  This bug has now been fixed in the CVS, although it took a little work to get there including an addendum to my original report. While this problem has now been corrected, you may still find the examples in the addendum helpful if you’re interested in getting a little extra out of the dispatch map through multiple signatures for an RPC method.

  • Python Module “re” Apr 15, 2005

    Posted by Mike Naberezny in Python

    Without question, one of my favorite bundled Python modules is re, a PCRE (Perl Compatible Regular Expression) engine.

    Python’s string object has many useful methods that I use frequently such as split(), strip(), replace(), and especially find(). When more sophisticated operations are needed, import re.

    A good deal of the work I do involves interpreting short messages sent from instrumentation. For most of these communications, I need to quickly evaluate a small quantity of data in three ways:

    1. Validate that the response is formatted as expected.
    2. Extract specific data from the response.
    3. Validate and then act on the data extracted.

    re‘s object-oriented interface is very nice, however I find that it can overcomplicate very small operations where a simple function like PHP’s preg_match or preg_match_all would be entirely sufficient. Luckily, identical functionality is easily recreated with re by cascading the re objects on one line and using the findall() method of the pattern object to return a list:

    import re
    testString = 'Light Switch = ON'
    matches = re.compile('Light Switch = (\w+)').findall(testString)
    if matches == []:
        # If an empty list is returned, the string under test
        # was nothing like we expected.
    elif matches[0] not in ('ON', 'OFF'):
        # The string under test was formatted as expected,
        # however the extracted data is unusual.
    else:
        # Success.  The string under test was formatted as
        # expected, and the expected data was extracted from it.