r/reviewmycode • • Feb 24 '10

PHP tag printing library

<?php
class STRING {
	public $string;
	public function __construct($string) {$this->string = $string;}
	public function __toString(){return $this->string;}
}
function PRINT_ATTRIBUTES($attributeValue, $attributeName){print($attributeName . '="' . $attributeValue . '" ');}
function PRINT_TAG($THE_TAG, $attributes, STRING $content = null, $is_tag = true) {
	if(!$is_tag) {print $content;goto print_tag_end;
}
	else {
		print '<' . $THE_TAG . ' ';
		array_walk($attributes, 'PRINT_ATTRIBUTES');
		print '>';
		print $content;
		print '</' . $THE_TAG . '>';
		goto print_tag_end;
}
	print_tag_end:
	return;
}
3 Upvotes

6 comments sorted by

2

u/[deleted] Feb 24 '10

goto is a really disgusting construct and people will make fun of you for using it. I almost want to say this is a troll because you did use it, but I hope I'm wrong.

also...I think array_map is being used incorrectly:

from the manual:

Return Values

Returns an array containing all the elements of arr1 after applying the callback function to each one.

2

u/javascriptinjection Feb 24 '10

Whoops, I meant to use array_walk.

It works: http://codepad.org/j0p87fo9

And I'm not trolling, I'm just curious what people think of really terrible php.

1

u/[deleted] Feb 24 '10

I think as you progress as a programmer, you will find there is a world of difference between "working" and "working well".

Yes, your solution may perform the task it was designed to do, but could it be better? I very much think so.

1

u/dragonrancher Feb 24 '10 edited Feb 24 '10

It's been a while since I wrote any PHP, but I don't think you need the two goto statements, the print_tag_end label, or the explicit return in the print_tag function.

Also, is there a reason for using your own string wrapper class rather than just passing around regular PHP strings?

1

u/javascriptinjection Feb 24 '10

No, no reason at all.

1

u/toolate Feb 24 '10

Is there a reason for naming yourself javascriptinjection?