PHP protected and an assault

<?php
class Victim {
  protected
$money = 100;

  public function
__toString() {
    return (string)
$this->money;
  }
}

class
Offender extends Victim {
  function
assault(Victim $victim) {
   
$victim->money = 0;
  }
}

$victim = new Victim;
$offender = new Offender;
$offender->assault($victim);
print (string)
$victim;
?>

»

PHP function calls returning references

Let's say we have this function:

<?php
function &collector() {
  static
$c = array();
  return
$c;
}
?>

we can use this like the following (very familiar to D7 coders):
<?php
 
// Note that $x = collector(); won't work.
 
$x = &collector();
 
$x[] = 'foo';
?>

Say you wanted to have one line code:
<?php
  array_push
(&collector(), 'foo');
?>

»

PHP function calls have quite some overhead

Apparently $array === (array) $array is significantly faster than is_array($array) for very small arrays. I have posted a detailed benchmark script which seems to concur: for approximately 40-50 bytes, like an array containing 5-6 integers (I am on 64 bit) or 5-6 of 8 byte strings this tricky is definitely faster.

»

foreach and references

Apparently this is not a bug and it's documented:

<?php
$array
= array('foo', 'bar');
foreach (
$array as &$foo);
foreach (
$array as $foo);
var_dump($array);
?>

»

Array reference always succeeds

php -r 'var_dump($a["foo"]);var_dump(&$a["foo"]);var_dump($a);'

»

Static properties and variable classnames

Oh no!

<?php
class test {
  static public
$foo = 'test1';
  static public
$bar = 'test2';
  function
foo() {
    print
"method foo\n";
  }
}

class
test1 {
  function
__construct() {
    print
"test1 constructor\n";
  }
}
class
test2 {
  function
__construct() {
    print
"test2 constructor\n";
  }
}

function
test1() {
  print
"test1 function\n";
}
function
test2() {
  print
"test2 function\n";
}
function
foo() {
  print
"foo function\n";
  return
'bar';
}

$foo = 'foo';

$a = test::$foo;
new
$a();
new
test::$foo();
$a();
print
test::$foo();
?>

»

On class properties

<?php
class foo  {
  static public
$bar = 2;
}

print
foo::$bar;
$n = new foo;
print
$n->bar;
print
$n->$bar;
?>

Results?

»

_FILES array

An <input type="file" name="foo"> produces this

<?php
array (
 
'foo' =>
  array (
   
'name' => '',
   
'type' => '',
   
'tmp_name' => '',
   
'error' => 4,
   
'size' => 0,
  ),
)
?>

$_FILES array. What do you think foo[bar] will produce?

»

array_slice confused about negative length

Why is the second array below empty? It seems array_splice cannot grok negative length, despite its docs.

<?php
$array
= array('a' => '1');

// GOOD
print_r(array_slice($array, 010));

// WTF. Why empty array?
print_r(array_slice($array, 0,  -10));
?>

Array
(
    [a] => 1
)
Array
(
)

Identical arrays vs equal arrays

There is a small problem when comparing arrays -- you can find arrays where array_diff comes back empty and yet, the arrays are not identical.

<?php
$array1
= array(
 
'foo' => 'foo',
 
'bar' => 'bar',
);

$array2 = array(
 
'bar' => 'bar',
 
'foo' => 'foo',
);

var_dump(array_diff($array1, $array2)); // Result: empty array.
var_dump($array1 == $array2); // Result: true.
var_dump($array1 === $array2); // Result: false.
?>

»
Syndicate content