Archive

Archive for the ‘programming’ Category

 <

Separate PHP Debug Output from HTML Page Content

April 23rd, 2009

I wrote a simple class today to help me organize my debug messages in PHP, while keeping the generated HTML intact (mostly). 

Personally, I like to output all the debug messages at the bottom of my html page (structurally speaking, and layout-wise). This way I can see how the output renders for real, and if something looks fishy I have the luxury of a potentially large set of debug messages that I can read through, looking for fishy stuff.

It’s just a glorified ‘echo’ function; but one which lets you decide where to actually output the debug messages in your generated HTML. Or you can think of it as a mini-logging function, that lasts for the time of just one HTTP response; and as a programmer you can do whatever you want with the ‘log’, i.e. output it any way you like.

In fact it is similar to what placeholders do for you within the Zend_View system, i.e. prepare some output but defer when it is actually outputted. In this case, the output is generated from within the controllers, models, and helper classes though, so that’s why I had to, in some sense, duplicate what ZF already provides for views.

Here is a quick tutorial:

First, download the DebugOutput class and rename the file to DebugOutput.php

In a strategic place in your code (for instance the init method of your controller in Zend Framework), include the php file and set the type of debug output you would like (none, inlined within <pre> … </pre>, inlined within <!– … –>, or deferred to your view):

...
public function init()
{
        require_once '..../DebugOutput.php';

	if ( ... /* if in development mode */ ... )
		DebugOutput::setOutputType(DebugOutput::DEFERRED_OUTPUT );
	else
		DebugOutput::setOutputType(DebugOutput::NO_OUTPUT );
	}
...

 

Then in your code, you call the _debug() function as often as needed:

...

_debug($thisVariable, 'this variable');

...

_debug($thatVariable, 'that variable');

...

 

Before you rendering engine / view engine / smarty / Zend_View takes over, you make the debug messages available to it in the following fashion (here assuming we are using a Zend Framework Action Controller, i.e. the Zend_Controller_Action class):

...
public function postDispatch()
{
        ...
    	$this->view->debugOutput = DebugOutput::getDeferredOutput();
}
...

 

And finally, at the bottom of your layout / wrapper / main view (in this case my Zend Framework layout), the debug messages are in an array, available to your rendering engine, and so you can treat them like any other output, for instance like this:

...
<?php if (!empty($this->debugOutput)) { ?>
<div style="background-color: #ffe0e0;
	width: 96%; padding-left: 2%; padding-right: 2%; padding-top: 50px; padding-bottom: 50px; margin-bottm: 100px;
	border: 1px dotted red; overflow: auto; float: none">
<p style="font-weight: bold; color: red; text-align: center">DEBUG INFO</p>
<pre>
<br />
<?php     if (is_array($this->debugOutput))
                    foreach ($this->debugOutput as $debugElement) { ?>
<hr />
<br />
<?= $debugElement['label'] ?>:
<?= print_r($debugElement['value'], true) ?>
<?php         } ?>
</pre>
</div>
<?php     } ?>
</body>
...

 

I hope someone finds it useful or gets some better ideas from it. If so, please share :-)

 

 <

Building iPhone Applications which Synchronize Local Data with a Server

April 22nd, 2009

Not an easy one to solve, for the moment. But doable yet:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/6239-database-sync-between-iphone-server.html

http://blogs.sybase.com/ithain/?p=577

Who else has reusable components for a solution?

 

 <

Programming the iPhone: native or web application?

April 3rd, 2009

We are regularly faced with the dilemma of suggesting a native iPhone application vs an iPhone-specific web application to our prospective clients. We find ourselves more at ease to develop complex applications in web-mode, and of course they can be derived from existing full-fledged web applications, and later be ported to other mobile platforms. In addition, Apple has made it possible to let the web apps act like native apps in many respects. However in some cases a native application is the only way to provide the desired level of integration with the iPhone operating system.

Here is a summary of the distinctions, taken straight from Apple’s iPhone developer’s web content.

Web Application on Safari Mobile version 4:

You cannot arbitrarily access the iPhone’s resources (the camera, GPS, audio, files, data, applications, etc.)

But:

You can make it look and feel like a native app:

How do I ensure that my web content uses all of the available screen space on iPhone?
How do I create a Home screen icon for my website or web application?
How do I hide the Safari on iPhone OS UI components when my web application is running?
How do I disable user zooming and scaling in my web application?
How do I detect iPhone orientation changes in my web application?
How do I conditionally load CSS that I have customized for iPhone?
How do I launch iPhone applications, like YouTube, iTunes, or Maps, to display content?
How do I dial a phone number from a webpage on iPhone?

(read the answers at Safari on iPhone User Experience Coding How-To’s)

You can make it view/play/perform content in native iPhone apps, a bit like a desktop browser plugin would, using Apple Url Scheme:

“Mail Links” describes the format for sending email with the Mail application.
“Phone Links” describes the format for dialing phone numbers in the Phone application.
“Text Links” describes the format for launching the Text application.
“Map Links” describes the format for specifying locations in the Maps application.
“YouTube Links” describes the format for linking to YouTube videos.
“iTunes Links” describes the format for linking to items in the iTunes Music Store.

(quoted from Apple URL Scheme Reference)

You can make it work offline:

… Safari provides an offline application cache. This cache allows you to create web-based applications that work correctly even when the user’s computer or web-enabled device is not connected to the Internet.
(quoted from HTML 5 Offline Application Cache)

… Safari supports the HTML 5 client-side storage specification.
(quoted from Key-Value Storage)

… Safari supports the HTML5 JavaScript database class. The JavaScript database class, based on SQLite, provides a very basic relational database intended for local storage of content that is too large to conveniently store in cookies (or is too important to accidentally delete when the user clears out his or her cookies).
(quoted from Using the JavaScript Database)

(get the complete PDF document at: Safari Client-Side Storage and Offline Applications Programming Guide)

Native iPhone application:

Unlike a web application, which runs in Safari, a native application runs directly as a standalone executable on an iPhone OS–based device. Native applications have access to all the features that make the iPhone interesting, such as the accelerometers, location service, and Multi-Touch interface. They can also save data to the local file system and even communicate with other installed applications through custom URL schemes.

(quoted from iPhone Reference Library)

Ulitzer.com has a some stats for an informal poll on LinkedIn about which (web or native) developers seem to favor:

Web or SDK?
— Web Development and SDK Development each offer distinct advantages to the iPhone Developer…So I set out over the last two months, with the aid of the LinkedIn Polls feature to gauge the trend

And btw they also have a simple summary of what you need to do to get your first iPhone application out there, starting from 0:

Seven Steps to the iPhone Developer’s World
… here are my seven steps to becoming an iPhone Developer. Tread carefully and you will become a wise man… Buy an Intel Based Mac & a Device – though the iPhone/iPod Touch device isn’t 100% necessary for this early stage, as the iPhone simulator that you will get with the SDK is free and more that capable for initial development …

I hope this helps clarify your options as a developer for the iPhone.