This
scripting addition from Late Night Software lets you manipulate
AppleScript records like real dictionaries and do set operations on
lists.
get user property "myage" in {myAge:42} --> 42
intersection of {1, 2, 3} and {2, 3, 4}
-- Result:
-- {
-- 2,
-- 3
-- }
PHP 5 seems to mostly fix the most
glaring problem in PHP 4, its non-intuitive handling of references.
However, this issue is more complicated than it sounds. For starters,
here is one of the unit tests from PHP 5:
<?php
class Foo {
var $name;
function Foo() {
$this->name = "I'm Foo!\n";
}
}
$foo = new Foo;
echo $foo->name;
$bar = $foo;
$bar->name = "I'm Bar!\n";
// In ZE1, we would expect "I'm Foo!"
echo $foo->name;
?>
With PHP 4, this prints:
I'm Foo!
I'm Foo!
With PHP 5, it prints:
I'm Foo!
I'm Bar!
To get this result in PHP 4, you need to use the &
operator:
<?php
class Foo {
var $name;
function Foo() {
$this->name = "I'm Foo!\n";
}
}
$foo = new Foo;
echo $foo->name;
$bar = &$foo;
$bar->name = "I'm Bar!\n";
// In ZE1, we would expect "I'm Foo!"
echo $foo->name;
?>
This is because PHP 4 copies objects by default; using &
makes it assign by reference instead of by copy. Copying by default is
pretty annoying if you’re used to other languages, but it does have the
virtue of consistency. Assignment without &
copies for
both scalars and objects; assignment with &
uses
references for both scalars and objects.
I’ve had trouble pinning down exactly how PHP 5 changes this. As far as
I can tell, object references are not mentioned in the official PHP 5 ChangeLog. One article I found says this:
In PHP4, references were a subject of confusion, the default behaviour,
when passing objects around, being to copy rather than reference the
object.
With PHP5 the default behaviour is now to pass objects by reference
(using the same approach as Java, in other words). What does this mean
to you, the developer? Well, if you didn't understand how references
worked in PHP4, you can now pretty much forget the subject completely.
But I find this confusing because the most confusing aspect of PHP 4
references goes unmentioned. In PHP 4, this code:
<?php
class Label {
var $text;
function Label($text) {
$this->text = $text;
}
}
$foo = new Label("foo");
$bar =& $foo;
$bar = new Label("bar");
print_r($foo);
?>
prints:
label Object
(
[text] => bar
)
This odd result is explained by the PHP manual thusly:
References in PHP are a means to access the same variable content by
different names. They are not like C pointers, they are symbol table
aliases. Note that in PHP, variable name and variable content are
different, so the same content can have different names. The most
close analogy is with Unix filenames and files - variable names are
directory entries, while variable contents is the file itself.
References can be thought of as hardlinking in Unix filesystem.
As noted, in PHP 4 objects and scalars were treated alike, hence this other example from the PHP manual:
$a = 1;
$b =& $a;
$b = 2;
print "$a $b";
// 2 2
When I first read about PHP 5, I thought that it made &
implicit for objects. In other words, I thought that:
<?php
class Label {
var $text;
function Label($text) {
$this->text = $text;
}
}
$foo = new Label("foo");
$bar = $foo;
$bar = new Label("bar");
print_r($foo);
?>
would print:
label Object
(
[text] => bar
)
in PHP 5. I thought this because I kept reading that you don’t have to type &
in PHP 5. However, it now appears that I was wrong. My tests with the just-released PHP 5 show that &
works in the same, arguably broken, “hardlink” kind of way with both PHP 4 and PHP 5. The difference is that in PHP 4, if you don’t use &
, a copy of the object is made. In PHP 5, if you don’t use &
, a Java-style reference is used. Thus, this code:
<?php
class Label {
var $text;
function Label($text) {
$this->text = $text;
}
}
$foo = new Label("foo");
$bar = $foo;
$bar = new Label("bar");
print_r($foo);
?>
prints:
label Object
(
[text] => foo
)
in both PHP 4 and PHP 5. And this code:
<?php
class Label {
var $text;
function Label($text) {
$this->text = $text;
}
}
$foo = new Label("foo");
$bar =& $foo;
$bar = new Label("bar");
print_r($foo);
?>
prints:
label Object
(
[text] => bar
)
in both PHP 4 and PHP 5.
The bad news is that PHP 5 is in some sense less consistent than before. There are now three different kinds of assignment (copy, hardlink, and reference copy), and scalars and objects are now treated differently. The good news is that, if I understand this correctly, you can banish &
from all your PHP 5 code and then use it like Java.
Language Design PHP Programming Top Posts