Sunday, 2 October 2011

Syntax :: the use of ?

I had a PHP exam this morning for a job interview, which I feel I probably failed, my main problem is that I had doubts on how PHP5 handles object references, I’ve been programming in C++ and objetive-C recently and my mind is stretch out with all the different IDEs, scoping, syntax and specifications of each language.


This causing me to go from one mindset to the other and in a written exam, were you don’t have access to documentation when in doubt or a debugger to feel out the language, it gets very difficult.


And so I’m reviewing the PHP5 documentation in hopes it will help me perform better at my next job interview.


While reading up, I found this very interesting proof of concept, the thing I like the most about it it’s the use of the syntax:


($bar === $this ? “Yes\n” : “No\n”)


Never used it or at least not everyday and it’s actually was a question on the exam.


At least when I fail, it’s always epic!



<?php

class Bar
{
public $prop = 42;
}

class Foo
{
public $prop = 17;
function boom()
{
$bar = &$this;
echo "\$bar is an alias of \$this, a Foo.\n";
echo '$this is a ', get_class($this), '; $bar is a ', get_class($bar), "\n";

echo "Are they the same object? ", ($bar === $this ? "Yes\n" : "No\n");
echo "Are they equal? ", ($bar === $this ? "Yes\n" : "No\n");
echo '$this says its prop value is ';
echo $this->prop;
echo ' and $bar says it is ';
echo $bar->prop;
echo "\n";

echo "\n";

$bar = new Bar;
echo "\$bar has been made into a new Bar.\n";
echo '$this is a ', get_class($this), '; $bar is a ', get_class($bar), "\n";

echo "Are they the same object? ", ($bar === $this ? "Yes\n" : "No\n");
echo "Are they equal? ", ($bar === $this ? "Yes\n" : "No\n");
echo '$this says its prop value is ';
echo $this->prop;
echo ' and $bar says it is ';
echo $bar->prop;
echo "\n";

}
}

$t = new Foo;
$t->boom();
?>

0 comments: