Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Tuesday, February 14, 2012

[php] PHP_Incomplete_Class

You've saved an object instance of YourClassName into session. On a subsequent retrieval of this object from session, you discover it's now a __PHP_Incomplete_Class object!

your_object = __PHP_Incomplete_Class Object
(
  [__PHP_Incomplete_Class_Name] => YourClassName
  [id] => 123
  [name] => Fred
)

Check where you're doing the session_start. Chances are you did not include YourClassName (plus any required files of its inheritance chain, if applicable) before starting the session.

Monday, February 13, 2012

[php] Show warnings/errors

I'm always forgetting this.

error_reporting(E_ALL);
ini_set('display_errors', '1');

Monday, November 21, 2011

[php] Content-type:image whitespace woes

So today I noticed the code for displaying scanned images was no longer working. This code basically boils down to:

header('Content-type: image/jpeg');
readfile('some_file.jpg');

Pretty simple, right? Yet somehow, even though some_file.jpg really does exist, and it really is a JPEG, I'm getting nothing. IE gives me the "image not found" red X icon, Chrome gives me a blank. What?

Rummaging around the web, I found a couple references to how unintentional whitespace output by your PHP script can screw your header() call into the ground. This seemed a likely explanation, but where (and why) is my PHP spitting out unwanted whitespace?

Turns out the culprit was lurking within these closing lines of an include:

  static function asOptionalJoin($fk = 'ipc') {
    $c = new static();
    return CriteriaJoin::optional($c, $fk);
  }
}
?> 

Do you see it? Trick question; it's invisible.

  static function asOptionalJoin($fk = 'ipc') {
    $c = new static();
    return CriteriaJoin::optional($c, $fk);
  }
}
?> <— look again

Yes, a space immediately to the right of the closing ?> PHP tag was the source of my grief. Take that space away, and the header call works again and the image springs back to life.

Interesting side note: one of the aforementioned resources explains that closing ?> tags are not only optional (didn't know this), but that some frameworks actually forbid them in PHP-only source files for the very reason of keeping accidental whitespace out of the output.

Sounds good to me; they're gone.

Tuesday, November 1, 2011

[php] Standard get() and getr()

Would you prefer $myarray['apple'] to return null when that index doesn't exist, rather than Notice: Undefined index: apple? How about $myobj->banana to return null rather than Notice: Undefined property: stdClass::$banana? Here's a simple getter to accommodate these fervent desires.

function get($e, $id, $default = null) {
  if (is_array($e))
    return isset($e[$id]) ? $e[$id] : $default;
  else if (is_object($e))
    return isset($e->$id) ? $e->$id : $default;
  else 
    return $default;
}
$result = get($myarray, 'apple');  // instead of $myarray['apple']
$result = get($myobj, 'banana');   // instead of $myobj->banana

Another useful getter is getr, which can navigate recursively through nested object/array hierarchies (and bail out gracefully at any point it runs into an undefined.)

function getr($e, $prop, $default = null) {
  if (strpos($prop, '.') !== false) {
    $props = explode('.', $prop, 2);
    $e0 = get($e, $props[0]);
    if ($e0 === null)
      return $default;
    else 
      return getr($e0, $props[1], $default);
  } else {
    return get($e, $prop, $default);
  }
}
$myrec1->name = array('first'=>'Joe','last'=>'Blow');
$myrec2->name = null;
echo getr($myrec1, 'name.last');  // returns 'Blow'
echo getr($myrec2, 'name.last');  // returns null

Sunday, October 30, 2011

[php] Calling parent's _construct()

To call the parent __construct() from an overridden constructor:

public function __construct() {
  $args = func_get_args();
  call_user_func_array(array(get_parent_class($this), '__construct'), $args);
  // do other stuff
}