With “people requesting”:http://www.procata.com/blog/archives/2004/07/29/goto-in-php/ @goto:@ statements for PHP, I thought it time I revealed the feature I’m wishing for ![]()
*I* want PHP to support…
p>. #ifdef … #endif preprocessor directives!
Why? Good question, let me explain my thinking…
I write a fair bit of debugging code in recent projects. Usually an @if (defined(’DEBUG’)@ statement containing @print@ statements; sometimes calls to a logging object/function (inside or without the @if@). None of which is needed when the code is in production.
Therefore if PHP included a preprocessor to remove these entries I could have faster execution, and save me having to run my files through a simple parser/regexp to remove such entries (OK, not done this yet, but I’ve been thinking of it).
4 comments ↓
This sounds like a premature optimisation to me. The overheard of handling an if (defined(…)) statement is so incredibly low that it almost certainly doesn’t justify an additional language feature. If it was causing a demonstratable performance overhead it would be a different matter, but ["Premature optimization is the root of all evil":http://c2.com/cgi/wiki?PrematureOptimization].
I think it’s time for php to rethink Arrays. Since Arrays are a tree-structure, has anybody made a move to give it the same features as the HTML or XML Dom?
Specifically, I’d like to search an array for a particular key, and grab its value, something like…
var $array = array();
$array = stuffThisArray();
$doughnut = $array.getElementById(’doughnut’);
Here’s my use case:
I’m working with CakePHP 1.2 and its pagination feature. I’d really, really like to have a view request data from its controller, nicely paginated. This is how it’s done…
From cake/app/views/involvements/glance.ctp (the view template, formerly glance.thtml)
…
$involvements = $this->requestAction(’involvements/glance/sort:date_from/direction:desc/limit:4′);
…
That command sends a request to the controller InvolvementController::glance(). The request shows up as the array $this->params(). So far, so good. I’d like to grab the sort, direction and limit. Problem is, the array looks like this:
…
Array ( [controller] => involvements [action] => glance [pass] => Array ( [sort] => date_from [direction] => desc [limit] => 4 ) [url] => Array ( [url] => involvements/glance/sort:date_from/direction:desc/limit:4 ) [bare] => 1 [webservices] => [return] => 1 [requested] => 1 [plugin] => [isAjax] => )
—
Yuk.
The feature makes it easy…
$limit = $array.getElementById(’limit’);
$sort = $array.getElementById(’sort’);
$direction = $array.getElemenbById(’direction’);
$paginate = array(
‘limit’ => $limit,
’sort’ => array($sort => $direction)
);
Hey Paul,
That is something you can easily implement, this took about 5 minutes to code out, note, it would probably need to be tested and optimized, but this is more like a proof of concept than anything else, you can get very detailed, and with 5.3 you could allow people to apply lamdas to the found element, etc:
class SuperArray {
public $array = array();
public function __construct($array = false) {
if ($array){
$this->array = $array;
}
}
public function push($item) {
array_push($this->array,$item);
}
public function pop() {
return array_pop($this->array);
}
//Etc etc
public function getElementById($id,$array = false) {
if (!$array){
$array = $this->array;
}
foreach ($array as $k=>$v) {
if ($k == $id){
return $v;
}
if (is_array($v)){
return $this->getElementById($id,$v);
}
}
return false;
}
}
$a = new SuperArray(array(”id1″ => “Hello”,”id2″ => array(”id3″ => “Yo peeps”,”id4″ => array(”id5″ => “And there we are”))));
echo $a->getElementById(’id5′);
echo $a->getElementById(’id3′);
And, to comment on the post, which I neglected to do in the first comment there.
Actually, what you are suggesting is a good idea, but perhaps it would be more palatable in this sense:
It would be nice if PHP came with the ability to compile into binary files, or to minify, not simply for performance, but for convenience, distributing one single sourcecode file instead of hundreds, which some software requires.
Nevertheless, this isn’t something that should or needs to be implemented by the core PHP team, something like this can in fact be written in PHP itself, and there’s no reason not too…Or, this could be written as a PHP module via Pear or Pecl or whatever.
Sorry for the double comments mate
Leave a Comment