Description
Description
use of static methods can get pretty bulky and leads to pretty long expressions, see
class HtmlHelper {
public static function escape_htmlspecial(string $string): string
{
return htmlspecialchars($string, \ENT_QUOTES, ENCODING_CHARSET, false);
}
}
?>
<div class="page-heading">
<h2><?php echo HtmlHelper::escape_htmlspecial($someString); ?></h2>
</div>
(html and php code would obvisouly be separated in 2 files.. putting them together for demonstration purposes)
static method have the benefit of beeing autoloaded though.
I could use namespaced functions instead:
namespace HtmlHelper {
function escape_htmlspecial(string $string): string
{
return htmlspecialchars($string, \ENT_QUOTES, ENCODING_CHARSET, false);
}
}
use function HtmlHelper\escape_htmlspecial as html_esc;
?>
<div class="page-heading">
<h2><?php echo html_esc($someString); ?></h2>
</div>
(html and php code would obvisouly be separated in 2 files.. putting them together for demonstration purposes)
that way I can use function
and optionally import aliases to shrink it even further.
this has the problem that I no longer can rely on native autoloading though.
its easier to read
I am thinking whether it would be possible to combine the benefits of the 2 approaches
Proposal:
class HtmlHelper {
public static function escape_htmlspecial(string $string): string
{
return htmlspecialchars($string, \ENT_QUOTES, ENCODING_CHARSET, false);
}
}
use function HtmlHelper::escape_htmlspecial as html_esc;
?>
<div class="page-heading">
<h2><?php echo html_esc($someString); ?></h2>
</div>
-> new use function
syntax (to be discussed)
-> for static methods only
-> autoloading of the HtmlHelper class
-> readability of the function imported aliases (import alias only optional)
wdyt?