Posts Tagged 'PHP'

PHP 5.3 RC2

The latest and greatest version of the PHP language is that much closer to the “Real World” with release candidate #2. PHP 5.3 includes a number of features that I have been waiting for, namely: Namespaces and Lambda functions.

As a person coming from a C++/C#/Java background, though, I thought the idea of using backslashes (/) as a namespace delimiter a bit clumsy and awkward. Since PHP syntax is similar to other “curly brace” languages, it would have made sense to me to use either the double-colon (::) or period (.). But after actually using this new language construct in PHP, the delimiter grew on me and my code wasn’t nearly as ugly as I thought it would be.

My only real complaint, now, is that my favorite PHP editor (PHPEclipse) is lagging behind. I have had to switch to PDT 2.1.0 instead.

SAjax for PHP… Revamped!

UPDATE: Someone else is moving forward with SAjax.  Since my code has deviated away from some of the SAjax design goals, I suggest you try his site out first if you’re interested in support for the original SAjax library.

I have been using SAjax 0.12 (by the folks at ModernMethod) for a couple of years now and I think it’s the greatest think since sliced bread.  SAjax allowed me to quickly learn the very exciting and helpful technology that is known as Ajax (asynchronous JavaScript and XML).  Ajax allows you to do all sorts of cool stuff (post data without a page refresh, get small HTML fragments to add to an existing document, etc).

SAjax is great, but I found that I was doing a lot of repetitive work and I wanted to simplify it even more.  This project is the result of my effort.  I have posted it on Google’s Code project for anyone who is interested in downloading it.  It can be found here.  Note: I believe this code will only work with PHP 5.2 or higher since it relies on json_encode/json_decode (although you might be able to add this via PECL).  I will be including more documentation and code examples at my leisure or if anyone wants it sooner ;-)

So what does this re-release include?  Here are some of the features:

  • Backwards compatible with Sajax.php 0.12.  I have only done basic testing to verify this, so if you encounter problems feel free to let me know.
  • New object-oriented interface via a singleton object: Sajax::Sajax()
  • Ability to export object methods to JavaScript.
  • Object-oriented JavaScript interface via the ‘sajax‘ object, e.g.: sajax.SomeMethod(cb)
  • Ability to pass DOM objects through via stubbed calls:, e.g. sajax.SomeMethod(this.form, cb)
  • Provides additional debugging information from the server side when debugging is turned on.
  • Provides interface for rendering additional JavaScript, either through externally referenced libraries or via code blocks.

Here is the obligitory implementation example:

<?php
/*
 * Demonstration of SAjax 0.14
 * Written by Kristian Oye
 * April 19th, 2009
 */
include_once("Sajax.php");

class TestClass
{
	public $ValueString = "Hello World";
	public $ValueInt = 42;

	public function GetString()
	{
		return $this->ValueString;
	}

	public function SajaxGetNumber()
	{
		return $this->ValueInt;
	}

	public function SajaxGetObject()
	{
		return $this;
	}

	public function SajaxCalculate($form, $op = 'add')
	{
		Sajax::Sajax()->AddDebugMessage("SajaxCalculate(".json_encode($form).", $op) called.");
		if($op === 'add')
			return ($form->param1 + $form->param2);
		elseif($op === 'subtract')
			return ($form->param1 - $form->param2);
		elseif($op === 'multiply')
			return ($form->param1 * $form->param2);
		elseif($op == 'divide')
			return ($form->param1 / $form->param2);
	}
}

function GetRandom()
{
	return rand(1, 100);
}

//  Create a class to handle some of our requests.
$foo = new TestClass();
$sjax = Sajax::Sajax();

//  Turn debugging on
$sjax->EnableDebug(true);

//  Export our callable methods
$sjax->Export(
	//  Export all 'Sajax' methods in TestClass
	$foo, 

	//  Export the callback to GetString in TestClass
	array($foo, 'GetString'),

	//  Simple method export
	'GetRandom',

	//  Non-existant methods
	'TestClass::GetError',
	'GetSomeError');

//  An additional script to include in our header.
$sjax->AddScriptReference('/static/example.js');

//  Handle the request if it is for AJAX or for a
//  JavaScript document, otherwise continue as normal.
$sjax->HandleRequest();

?>
<html>
	<head>
		<title>Sajax Test</title>

		<?php $sjax->ExportIncludeJS(); ?> 

		<script type="text/javascript">
		function cb_Calculate(result)
		{
			document.getElementById('CalcResult').innerHTML = result;
		}
		</script>
	</head>

<body>
<table>
<tr>
<td>Get a string:</td/>
<td colspan="2">
				<input	type="button"
						value="Go"
						onclick="sajax.GetString(function(result) { alert(result);  })" /></td>
</tr>
<tr>
<td>Get a number:</td/>
<td colspan="2">
				<input	type="button"
						value="Go"
						onclick="sajax.GetNumber(function(result) { alert(result);  })" /></td>
</tr>
<tr>
<td>Get a random:</td/>
<td colspan="2">
				<input	type="button"
						value="Go"
						onclick="sajax.GetRandom(function(result) { alert(result);  })" /></td>
</tr>
<tr>
<td>Get an object:</td/>
<td colspan="2">
				<input	type="button"
						value="Go"
						onclick="sajax.GetObject(function(result) { alert(result);  })" /></td>
</tr>
<tr>
<td>Get an ERROR:</td/>
<td colspan="2">
				<input	type="button"
						value="Go"
						onclick="sajax.GetError(function(result) { alert(result);  })" /></td>
</tr>
<tr>
<td>ANOTHER error:</td/>
<td colspan="2">
				<input	type="button"
						value="Go"
						onclick="sajax.GetSomeError(function(result) { alert(result);  })" /></td>
</tr>
<tr>
			<form action="/bogus" method="get">
<td style='vertical-align:top'>Calculate:</td>
<td style='vertical-align:top'>
					<input	type="text"
							value="0"
							id="param1"
							name="param1" /></td>
<td>
					<input	type="button"
							value=" + "
							onclick="sajax.Calculate(this.form, 'add', cb_Calculate)" />

					<input	type="button"
							value=" - "
							onclick="sajax.Calculate(this.form, 'subtract', cb_Calculate)" />
					<input	type="button"
							value=" * "
							onclick="sajax.Calculate(this.form, 'multiply', cb_Calculate)" />

					<input	type="button"
							value=" / "
							onclick="sajax.Calculate(this.form, 'divide', cb_Calculate)" /></td>
<td style='vertical-align:top'>
					<input	type="text"
							value="0"
							id="param2"
							name="param2" /> = <span id="CalcResult"></span></td>
</form></tr>
</table>
</body>
</html>

Easter ‘Saturday’

It was not quite Easter yet, but I did get the opportunity to spend some time with Kristin on this holiday weekend. We went to the bookstore and picked out a neat Easter book with pop-up pages and Kristin wanted a box of cute, little baby chickens (I liked them more than the monster chocolate egg she was eyeing). I also bought her an Easter outfit so we could take this nice Father/Daughter picture.

In other news, I have been working on a political campaign website lately and it has me thinking about the possibility of starting a part-time, side business: web consulting. I am learning a host of new technologies that have many practical applications in the web development world. The following technologies are ones that I have started to incorporate into my skill set (and that I would suggest to other web developers):

AJAX
Ajax is a powerful technology that uses JavaScript and XML to communicate between an existing document and a web server (e.g. database). Ajax is nice, in my opinion, because it allows you to make asynchronous calls and to update existing DOM objects without a complete page refresh.
JSON
JavaScript Object Notation allows for objects to be serialized into JavaScript strings that can be evaluated into JavaScript objects… and back again! When used in conjunction with Ajax this can be a very powerful tool. Facebook is a prime example of this!
JQuery
JQuery is a powerful library of JavaScript routines that allow you to manipulate DOM objects in a variety of useful ways. So far, I have primarily been using JQuery ’selectors’ to dynamically change CSS classes on DOM objects (useful for dropdown menus, etc).

Spoke Too Soon

Well, summer WAS here briefly, but I guess I spoke too soon.  It was back to gray and yucky again today.  Anyway!

I should get to bed, but I just had to say I finally accomplished my goal of getting Apache 2.2 and PHP 5 to run on Windows tonight… it only took like TWO HOURS!  It all came down to one optional module I tried to install during the PHP install process:

[PHP_THREADS]
extension=php_threads.dll

Ugh.  Pages would serve up half way and then PHP would make Apache throw a nasty exception… it went so far as to kill the server process.  After a lot of trial and error (taking modules out, puting them back in, etc) I finally narrowed it down to this one.

I was actually hoping to have access to threading, too, but whatever.  My target development platform is Linux anyway… but I was hoping to develop on Windows.  Hmm.  Fine if I don’t use threads, I guess?


a

 

December 2009
M T W T F S S
« Nov    
 123456
78910111213
14151617181920
21222324252627
28293031